2012-09-13 64 views
0

如何在Silverlight中重新創建以下代碼?談到異步時我很迷茫。Silverlight中的HttpWebRequest

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; 
    } 

回答

0
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; 
    } 
} 
+0

我不認爲WebClient的可替代的HttpWebRequest。您可以使用HttpWebRequest而不是WebClient,但不能用其他方式替換它。 – nubm

相關問題