Windows Phone获取服务器源代码

问题描述:

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

I am trying to get the source code of a site. In windows application a simple http request would be enough. However in windows phone it is a lot more complicated. I searched a lot on google and didn't come with a clear answer. This is what I tried but without big success.

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在返回时仍具有空字符串的默认值.

The problem is that you return sReturn immediately, but the download won't complete until some time in the future. So sReturn still has the default value of the empty string at the time you return it.

您可以下载此示例,其中包括执行此操作的代码正是您要使用HttpClient可移植库执行的操作.

You can download this sample which includes code for doing exactly what you want to do using the HttpClient portable library.