我正在使用此代码在我的 Metro Style 应用程序中下载网页:
public static async Task<string> 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)
运行时,它会抛出一个异常,上面写着:
An error occurred while sending the request.
从类型:HttpRequestException
.
编辑:
我使用 InnerException 来获取更多信息。抛出的第一个异常是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 以查看它是否能解决我的问题。