Logo

C# 基础连接已经关闭: 发送时发生错误

photo

2022年05月10日

前几日对接别的平台时,发送的请求是https,发送请求时会遇到“基础连接已经关闭: 发送时发生错误”的问题。看到了平台上的问题解答,发现这个方法挺好用,就在此记录一下。

/// <summary>
        /// 指定Post地址使用Get 方式获取全部字符串
        /// </summary>
        /// <param name="url">请求后台地址</param>
        /// <returns></returns>
        public static string Post(string url, string paramString)
        {
            string result = "";
            //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebRequest request = null;
            //如果是发送HTTPS请求 
            if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                //关于ServicePointManager.SecurityProtocol的设置是解决问题的关键。
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3;
                //.Net4.0
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                //.Net4.5
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls1.2;
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version10;

            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
            request.Timeout = 5000;
            request.Method = "POST";
            request.ContentType = "application/json";
            #region 添加Post 参数

            byte[] data = Encoding.UTF8.GetBytes(paramString);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受 
        }

 

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

Windows Server IIS+ARR反向代理(配置反向代理服务器) 1.概念说明:反向代理反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相...WindowsServerIIS+ARR反向代理(配置反向代理服务器) 作者:Pastore Antonio
1618 浏览量
ffmpeg 生成水印 1:先要配置ffmpeg的滤镜:参考:https://www.jianshu.com/p/9d24...ffmpeg生成水印 作者:Pastore Antonio
1527 浏览量
Navicat Premium 12.0.22 安装与破解 一、安装  NavicatPremium12.0.22的下载链接:https://pan.ba...NavicatPremium12.0.22安装与破解 作者:Pastore Antonio
1472 浏览量
谷歌浏览器扩展程序报错 The message port closed before a response was received. 问题描述ChromeExtension报错:Uncheckedruntime.lastEr...谷歌浏览器扩展程序报错Themessageportclosedbeforearesponsewasreceived. 作者:Pastore Antonio
1463 浏览量
天地光阴,唯深情可依 如果光阴把一切席卷而去,最后剩下的,一定是一抹幽兰。如果爱情把一切席卷而去,最后留下的,也定是带着蓝色记忆的最初的心动。 作者:Pastore Antonio
1460 浏览量