c# https get请求 基础连接已关闭,接受时发生错误 解决方法

 
 
用HttpWebRequest模拟发送https请求,提示错误: 基础连接已关闭,接受时发生错误
在本地测试ok,开发环境为64位win7 vs2013 发布到windows server 2008 x86系统上运行一直报错。。
下载2008 r2 x64运行发现数据正常。。

public static string GetUrl(string url)
        {
          
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                System.Console.WriteLine("https connections.....");
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                // 这里设置了协议类型。
                //ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;// (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2; 
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //(SecurityProtocolType)768 | (SecurityProtocolType)3072
              
                ServicePointManager.CheckCertificateRevocationList = true;
                ServicePointManager.DefaultConnectionLimit = 100;
                ServicePointManager.Expect100Continue = false;
            }
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            //增加下面两个属性即可  
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
           
            request.Method = "GET";    //使用get方式发送数据
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = "";
            request.AllowAutoRedirect = true;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Accept = "*/*";

            //获取网页响应结果
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //Thread.Sleep(2000);
            Stream stream = response.GetResponseStream();
            //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            string result = string.Empty;
            using (StreamReader sr = new StreamReader(stream))
            {
                result = sr.ReadToEnd();
            }

            if (request != null)
            {
                request.Abort();
            }

            if(response !=null)
            {
                response.Close();
            }

            return result;
        }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值