在 WinRT 中下载网页引发异常

发布于 2024-12-18 05:47:07 字数 1437 浏览 1 评论 0原文

我正在使用此代码在我的 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发送请求时发生错误。 来自类型:HttpRequestException

编辑:

我使用 InnerException 来获取更多信息。抛出的第一个异常是 SocketException,并显示以下消息:

尝试以访问权限禁止的方式访问套接字

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

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

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

错误显示在图片

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2024-12-25 05:47:07

好的。我找到了答案。例外是因为我在 Windows 8 Developer Preview 上安装了 NetLimiter 产品,但它以某种方式阻止了我的应用程序访问互联网。

更新:
首先,这是在 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 "";
        }
    }

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

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

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

白龙吟 2024-12-25 05:47:07

非常确定 netlimiter 正在通过本地主机代理运行您的互联网请求,并且您的应用程序没有允许此类访问本地网络的“本地网络”功能

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文