【问题标题】:Downloading webpages in WinRT throws an exception在 WinRT 中下载网页会引发异常
【发布时间】:2012-01-08 05:10:36
【问题描述】:

我正在使用此代码在我的 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,看看它是否能解决我的问题。

【问题讨论】:

  • 您是否确认该 URL 是您真正想要的?将 URL 粘贴到不同的用户代理(浏览器等)中,以确保它确实以您期望的方式回复。
  • 我已经使用http://www.google.com 对其进行了测试。我确定网址没问题。
  • 请查看编辑后的帖子。也许新信息会有所帮助。
  • 只是为了它,你能尝试用WebRequestHandler替换HttpClientHandler,这是一个更派生的类吗?
  • 请看这个网址:sharpgis.net/post/2011/10/05/…

标签: c# .net windows-runtime


【解决方案1】:

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

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

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

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

【讨论】:

  • 我感觉是防火墙问题。 ;)
  • 谢谢。我不得不重新安装我的操作系统来调查这个。你的回答让我开始思考。
  • @AlirezaNoori,我遇到了异常:“无法在 Windows 8 Metro App 中创建 SSL/TLS 安全通道”有什么想法吗?网址:stackoverflow.com/questions/14158069/…
  • @Somnath 您的链接是否以https 开头?我检查并得到了https 的错误(因此是 SSL 错误)。尝试使用简单的http 看看它是否有效。
  • 当您将互联网选项设置为通过代理服务器时,也会发生这种情况(正如我刚刚发现的那样)。
【解决方案2】:

很确定 netlimiter 正在通过 localhost 代理运行您的互联网请求,而您的应用没有“本地网络”功能,无法访问本地网络

【讨论】:

    猜你喜欢
    • 2013-01-13
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多