最近接入腾讯云的短信平台,平台的接口是HTTPS的,开发环境是Windows,而生产环境是Linux(CentOS)。在开发调试的时候都正常通过,没问题;而部署到CentOS上之后,会报System.PlatformNotSupportedException: The handler does not support custom handling of certificates with this combination of libcurl (7.29.0) and its SSL backend ("NSS/3.34").错误。这个问题在网上找了一圈,好不容易Google到一个俄文的Blog,里面给出了一个解决方法,但是博主特别强调了一下,这是一个非常糟糕的方法:

yum update
yum install openssl-devel gcc
wget https://curl.haxx.se/download/<latest>.tar.gz
tar -zxf <latest>.tar.gz
cd <latest>
./configure --prefix=/usr/local/curl/ --without-nss --with-ssl=/usr/local/ssl/
make && make install 
 
echo '/usr/local/curl/lib' > /etc/ld.so.conf.d/libcurl.conf && ldconfig 

https://curl.haxx.se/download/.tar.gz 是curl的最新版本下载地址,目前是
https://curl.haxx.se/download/curl-7.60.0.tar.gz
之后就可以正常提交了。
感谢原作者提供的方法,原博客:https://ales79.blogspot.com/2018/06/centos-7-libcurl-with-openssl-backend.html

我使用的Post方法如下:

        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            //直接确认,否则打不开    
            return true;
        }

        /// <summary>
        /// Post提交数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="url">目标地址</param>
        /// <param name="contentType">数据类型</param>
        /// <param name="charset">编码</param>
        /// <returns></returns>
        public static string Post(string data, string url,string contentType,string charset= "utf-8")
        {
            System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接

            string result = "";//返回结果

            int timeout = 30;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream reqStream = null;

            try
            {
                //设置最大连接数
                ServicePointManager.DefaultConnectionLimit = 200;                
                //设置https验证方式
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                            new RemoteCertificateValidationCallback(CheckValidationResult);
                }

                /***************************************************************
                * 下面设置HttpWebRequest的相关属性
                * ************************************************************/
                request = (HttpWebRequest)WebRequest.Create(url);

                request.Method = "POST";
                request.Timeout = timeout * 1000;

                ////设置代理服务器
                //WebProxy proxy = new WebProxy();                          //定义一个网关对象
                //proxy.Address = new Uri(WxPayConfig.PROXY_URL);              //网关服务器端口:端口
                //request.Proxy = proxy;

                //设置POST的数据类型和长度
                request.ContentType = $"{contentType};charset={charset}";
                byte[] res = System.Text.Encoding.GetEncoding(charset).GetBytes(data);
                request.ContentLength = res.Length;

                //往服务器写入数据
                reqStream = request.GetRequestStream();
                reqStream.Write(res, 0, res.Length);
                reqStream.Close();

                //获取服务端返回
                response = (HttpWebResponse)request.GetResponse();

                //获取服务端返回数据
                StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(charset));

                result = sr.ReadToEnd().Trim();
                sr.Close();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                System.Threading.Thread.ResetAbort();
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    
                }
            }
            catch (Exception e)
            {
                
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            return result;
        }
Last modification:July 1, 2018
如果觉得我的文章对你有用,请随意赞赏