• HttpWebRequest.GetResponse()操作超时


        在项目中调用了一条比较复杂的sql 写的web api,调用的时候经常操作超时。

       修改HttpWebRequest.Timeout与HttpWebRequest.KeepAlive

       具体相关代码如下:

         /// <summary>  
            /// 创建GET方式的HTTP请求  
            /// </summary>  
            public static HttpWebResponse CreateGetHttpResponse(string url, int timeout, string userAgent, CookieCollection cookies)
            {
                HttpWebRequest request = null;
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    //对服务端证书进行有效性校验(非第三方权威机构颁发的证书,如自己生成的,不进行验证,这里返回true)
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(url) as HttpWebRequest;
                    request.ProtocolVersion = HttpVersion.Version10;    //http版本,默认是1.1,这里设置为1.0
                }
                else
                {
                    request = WebRequest.Create(url) as HttpWebRequest;
                }
                request.Method = "GET";
    
                //设置代理UserAgent和超时
                //request.UserAgent = userAgent;
                //request.Timeout = timeout;
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }
                try
                {
                    request.Timeout = Timeout.Infinite;
                    request.KeepAlive = true;
                    return request.GetResponse() as HttpWebResponse;
                }
                catch (WebException ex)
                {
                    return (HttpWebResponse)ex.Response;
    
                }
            }
  • 相关阅读:
    键值表
    CRC校验方法
    extern 使用
    编码格式简介(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
    学习积累
    二分查找写法
    生活技术常识
    JAVA实例
    JAVA_Sprint学习(一)
    手把手使用Git?
  • 原文地址:https://www.cnblogs.com/binzi/p/12325356.html
Copyright © 2020-2023  润新知