Silverlight中的HttpWebRequest
如何在Silverlight中重新创建以下代码?谈到异步,我非常迷茫Silverlight中的HttpWebRequest,silverlight,silverlight-4.0,Silverlight,Silverlight 4.0,如何在Silverlight中重新创建以下代码?谈到异步,我非常迷茫 public class StringGet { public static string GetPageAsString(Uri address) { string result = ""; // Create the web request HttpWebRequest request = WebRequest.Create(address) a
public class StringGet
{
public static string GetPageAsString(Uri address)
{
string result = "";
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Read the whole contents and return as a string
result = reader.ReadToEnd();
}
return result;
}
我不认为WebClient可以取代HttpWebRequest。你可以使用HttpWebRequest而不是WebClient,但你不能用另一种方式替换它。他问的是HttpWebRequest,而不是WebClient。
public void DownloadHtml(Uri address){
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += WebClientDownloadString_Complete;
webClient.DownloadStringAsync(address)
}
private void WebClientDownloadString_Complete(object sender, DownloadStringCompletedEventArgs e) {
if (e.Error == null) {
string html = e.Result;
}
}