在WinRT的下载网页抛出一个异常 [英] Downloading webpages in WinRT throws an exception

查看:288
本文介绍了在WinRT的下载网页抛出一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code,下载一个网页在我的Metro风格的应用程序:

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;
    }

问题是,当行 client.GetAsync(URL)运行时,它抛出一个异常,说:

the problem is that when the line client.GetAsync(url) runs, it throws an exception that says:

发送请求时发生错误从类型:的Htt prequestException

编辑:

我使用的InnerException以获得更多信息。抛出的第一个例外是 SocketException 与下面的消息:

I've used the InnerException to get more info. The first exception thrown is SocketException with the message below:

试图访问套接字由它的访问权限禁止的一种方式

在System.Net.HttpWebRequest.EndGetResponse(IAsyncResult的asyncResult)

在System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult的AR)

编辑2: 我已经下载并运行微软的样品,我也得到了同样的错误: HTTP://$c$c.msdn.microsoft.com / windowsapps / HttpClient的上传样本,f1abcc4e

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

编辑3: 我跑这在另一台机器,它能正常工作。所以,我想没有什么不对的code。我复制的解决方案,这意味着有什么不妥的解决办法。我试图重新安装Windows开发preVIEW,看它是否解决了我的问题。

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.

推荐答案

确定。我找到了答案。唯一的例外是,因为我在Windows 8开发preVIEW安装NetLimiter产品,并不知何故,$ P $访问互联网pvented我的应用程序。

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.

更新: 首先,这是code中的每个版本的Windows 8的工作。

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 "";
        }
    }

其次,如果这也不行,有机会,你有一个网络相关软件preventing您的应用程序来访问互联网。一种流行的情况下是 proxifier 。如果您已经安装了proxifier的您MetroStyle应用程序将无法访问互联网的要使它发挥作用,请参考我的博客:

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/一般/使用-proxifier功能于Windows的8且不fiddler2

这篇关于在WinRT的下载网页抛出一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆