HTTPS Web请求失败 [英] HTTPS web request failing

查看:243
本文介绍了HTTPS Web请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行包含的第一个HTTPS请求下面的程序成功,但第二个请求失败。这两个网址是有效的,两者都可以成功地在浏览器中访问。做任何建议,有什么需要成功访问第二个URL?

 使用系统;
使用System.IO;
使用System.Net;

公共类节目
{
    私有静态无效的主要(字串[] args)
    {
        VAR内容=;
        BOOL状态;
        VAR为url1 =htt​​ps://mail.google.com;
        VAR URL2 =htt​​ps://my.ooma.com;
        状态= DoHtt prequest(为url1,出来的内容);
        OutputStatus(URL1,地位,内容);
        状态= DoHtt prequest(URL2,出来的内容);
        OutputStatus(URL2,地位,内容);
        到Console.ReadLine();
    }

    私有静态无效OutputStatus(字符串URL,布尔状态,字符串内容)
    {
        如果(状态)Console.WriteLine(URL = {0},状态=成功,内容长度= {1},网址,content.Length);
        其他Console.WriteLine(URL = {0},状态=失败,的ErrorMessage = {1},URL,内容);
    }

    私有静态布尔DoHtt prequest(字符串URL,出字符串内容)
    {
        内容=;
        VAR请求=(HttpWebRequest的)WebRequest.Create(URL);
        尝试
        {
            request.Method =GET;
            request.CookieContainer = NULL;
            request.Timeout = 25000; //25秒
            变种响应=(HttpWebResponse)request.GetResponse();
            VAR的StreamReader =新的StreamReader(response.GetResponseStream());
            含量= streamReader.ReadToEnd();
            返回true;
        }
        赶上(WebException前)
        {
            内容= ex.Message;
            返回false;
        }
    }
}
 

解决方案

从历史上看,这说明大多数的问题,我已经看到了,当你忘记调用.Close()的对象上发生的GetResponseStream()返回。这个问题的存在,因为当你忘记关闭的第一个请求,第二个请求死锁等待一个可用连接。

这通常挂发生在3日的要求,而不是第二。

更新:看你的摄制,这无关的请求的顺序。你打一个问题,因为这个网站是发送一个TLS警告在HTTPS握手的开始,当发生.NET将超时。请参阅<一href="http://blogs.msdn.com/b/fiddler/archive/2012/03/29/https-request-hangs-.net-application-connection-on-tls-server-name-indicator-warning.aspx" rel="nofollow">http://blogs.msdn.com/b/fiddler/archive/2012/03/29/https-request-hangs-.net-application-connection-on-tls-server-name-indicator-warning.aspx.这个问题只在Windows Vista repros和更高,因为该警告涉及不在在WinXP将HTTPS堆存在一个TLS扩展

When I run the program contained below the first HTTPS request succeeds, but the second request fails. Both url's are valid and both can be accessed successfully in a browser. Any suggestions as to what needs to be done to access the second url successfully?

using System;
using System.IO;
using System.Net;

public class Program
{
    private static void Main(string[] args)
    {
        var content = "";
        bool status;
        var url1 = "https://mail.google.com";
        var url2 = "https://my.ooma.com";
        status = DoHttpRequest(url1, out content);
        OutputStatus(url1, status, content);
        status = DoHttpRequest(url2, out content);
        OutputStatus(url2, status, content);
        Console.ReadLine();
    }

    private static void OutputStatus(string url, bool status, string content)
    {
        if (status) Console.WriteLine("Url={0}, Status=Success, content length = {1}", url, content.Length);
        else Console.WriteLine("Url={0}, Status=Fail, ErrorMessage={1}", url, content);
    }

    private static bool DoHttpRequest(string url, out string content)
    {
        content = "";
        var request = (HttpWebRequest) WebRequest.Create(url);
        try
        {
            request.Method = "GET";
            request.CookieContainer = null;
            request.Timeout = 25000; // 25 seconds
            var response = (HttpWebResponse) request.GetResponse();
            var streamReader = new StreamReader(response.GetResponseStream());
            content = streamReader.ReadToEnd();
            return true;
        }
        catch (WebException ex)
        {
            content = ex.Message;
            return false;
        }
    }
}

解决方案

Historically, most problems of this description that I've seen occur when you forget to call .Close() on the object returned from GetResponseStream(). The problem exists because when you forget to close the first request, the second request deadlocks waiting for a free connection.

Typically this hang happens on the 3rd request, not the second.

Update: Looking at your repro, this has nothing to do with the order of the requests. You're hitting a problem because this site is sending a TLS Warning at the beginning of the HTTPS handshake, and .NET will timeout when that occurs. See http://blogs.msdn.com/b/fiddler/archive/2012/03/29/https-request-hangs-.net-application-connection-on-tls-server-name-indicator-warning.aspx. The problem only repros on Windows Vista and later, because the warning is related to a TLS extension that doesn't exist in the HTTPS stack on WinXP.

这篇关于HTTPS Web请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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