在WinRT中下载网页会引发exception

我正在使用此代码在Metro Style应用程序中下载网页:

public static async Task DownloadPageAsync(string url) { HttpClientHandler handler = new HttpClientHandler(); handler.UseDefaultCredentials = true; handler.AllowAutoRedirect = true; HttpClient client = new HttpClient(handler); HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); string responseBody = response.Content.ReadAsString(); return responseBody; } 

问题是当行client.GetAsync(url)运行时,它会抛出一个exception,说:

An error occurred while sending the request. 来自类型: HttpRequestException

编辑:

我已经使用InnerException来获取更多信息。 抛出的第一个exception是SocketException ,消息如下:

An attempt was made to access a socket in a way forbidden by its access permissions

at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)

at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)

编辑2:我已经下载并运行了Microsoft的示例,我得到了同样的错误: http : //code.msdn.microsoft.com/windowsapps/HttpClient-Upload-Sample-f1abcc4e

错误显示在图片中

编辑3:我在另一台机器上运行它,它工作正常。 所以我猜这个代码没什么问题。 我复制了解决方案,这意味着解决方案也没有任何问题。 我正在尝试重新安装Windows Developer Preview以查看它是否解决了我的问题。

好。 我找到了答案。 例外是因为我在Windows 8开发人员预览版上安装了NetLimiter产品,并以某种方式阻止了我的应用程序访问互联网。

更新:首先,这是在每个版本的Windows 8中运行的代码。

  public static async Task DownloadPageAsync(string url) { try { HttpClientHandler handler = new HttpClientHandler { UseDefaultCredentials = true, AllowAutoRedirect = true }; HttpClient client = new HttpClient(handler); HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); string html = await response.Content.ReadAsStringAsync(); return html; } catch (Exception) { return ""; } } 

其次,如果这不起作用,您可能会有一个与网络相关的软件阻止您的应用访问互联网。 一个流行的案例是proxifier 。 如果您安装了proxifier, 您的MetroStyle应用将无法访问互联网。 要使其工作,请参阅我的博客:

http://anoori.me/blog/general/use-proxifier-in-windows-8-without-fiddler2

非常肯定netlimiter通过localhost代理运行您的Internet请求,并且您的应用程序没有“本地网络”function,这将允许此类访问本地网络