我正在尝试获取网站的源代码。在Windows应用程序中,一个简单的http请求就足够了。但是在Windows Phone中,它要复杂得多。
我在Google上搜索了很多内容,但没有明确的答案。
这是我尝试过的,但没有取得很大的成功。

public static sReturn = "";

private string _InetGetSourceCode(string sUrl)
{
   _InetReadEx(sUrl);
   return sReturn;
}

private void _InetReadEx(string sUrl)
{
   WebClient client = new WebClient();

   client.DownloadStringCompleted += new
   DownloadStringCompletedEventHandler(DownloadStringCallback2);
   client.DownloadStringAsync(new Uri(sUrl));
}

private static void DownloadStringCallback2(Object sender,DownloadStringCompletedEventArgs e)
{
   if (!e.Cancelled && e.Error == null)
   {
      sReturn = e.Result;
   }
}

我在这里做错了什么?

最佳答案

问题是您立即返回sReturn,但是下载要等到将来某个时候才能完成。因此,sReturn在返回时仍具有空字符串的默认值。

您可以下载this sample,其中包含使用HttpClient可移植库完全执行您想做的代码。

关于c# - Windows Phone获取服务器源代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32020021/

10-11 16:44