سؤال

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

The error is shown in the picture

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.

هل كانت مفيدة؟

المحلول

OK. I found the answer. The exception was because I installed NetLimiter product on Windows 8 Developer Preview and somehow it prevented my app from accessing the internet.

UPDATE: First of all, this is the code working in EVERY version of Windows 8.

    public static async Task<string> 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 "";
        }
    }

Secondly, if this doesn't work, chances are, you have a network-related software preventing your app to access the Internet. One popular case is proxifier. If you have proxifier installed your MetroStyle apps won't be able to access the internet. To make it work, please refer to my blog at:

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

نصائح أخرى

pretty sure netlimiter was running your internet request through a localhost proxy, and your app didn't have the "local network" capability which would allow such access to the local network

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top