I'm using this code to download a web page in my Metro Style app:
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;
}
the problem is that when the line client.GetAsync(url)
runs, it throws an exception that says:
An error occurred while sending the request.
From type: HttpRequestException
.
EDIT:
I've used the InnerException to get more info. The first exception thrown is SocketException
with the message below:
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)
EDIT 2: I've downloaded and ran the sample from Microsoft and I get the same error: http://code.msdn.microsoft.com/windowsapps/HttpClient-Upload-Sample-f1abcc4e
EDIT 3: I ran this on another machine and it worked fine. So I guess there's nothing wrong with the code. I copied the solution and it means that there's nothing wrong with the solution either. I'm trying to re-install Windows Developer Preview to see if it fixes my problem.