Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a Asp.Net project, I used System.Net (FW 4.030319). In my Silverlight project, I found the System.Net (also FW 4.030319) contains something different from that used in Asp.Net. I tried to load the .dll used in asp.net but got error warning that prohibits me to use the component out of Silverlight. What I want to do it to use the method below:
C#
System.Net.WebClient client = new System.Net.WebClient();
string downloadedString = client.DownloadString("http://xxxx");

In the SL version does not contain the .DownloadString() method. Then I revised my code like that below:
C#
WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(readComplete);
...
private void readComplete(object sender, OpenReadCompletedEventArgs e) {
    byte[] buffer = new byte[e.Result.Length]; //crashes here with exception
}

However, when I run debugging, it never get the break point in the void readComplete().
How should I do in order to get the string result through a URL in SL? Thanks.
Posted
Updated 11-May-15 5:16am
v2
Comments
s yu 11-May-15 12:57pm    
referring to http://blogs.msdn.com/b/silverlight_sdk/archive/2008/04/01/using-webclient-and-httpwebrequest.aspx, I revised my code and use
Uri url_ = new Uri(url);
WebClient client = new WebClient();
client.DownloadStringAsync(url_);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
...
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { ... }
The problem is solved. Thanks for your review.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900