HttpWebRequest 请求HTTPS协议时报错:The requested security protocol is not supported. (不支持请求的安全协议)

public String Get(string url, byte[] data)
    {

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
        try
        {
            //服务器需安装.net4.5,否则报错:The requested security protocol is not supported. (不支持请求的安全协议)
            ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            //SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 
            //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;

            req.ProtocolVersion = HttpVersion.Version11;
            req.Method = "POST";
            req.KeepAlive = false;
            //req.Timeout = 1200000;
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            //req.Headers.Add("ContentType", "application/x-www-form-urlencoded");
            req.Headers.Add("appKey", appKey);
            req.Headers.Add("appSecret", appSecret);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
            }
            using (WebResponse response = req.GetResponse())
            {
                string text = "";
                using (Stream responseStm = response.GetResponseStream())
                {
                    StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8);
                    text = redStm.ReadToEnd();
                }
                response.Close();
                //response = null;
                req.Abort();
                return text;
            }
        }
        catch (Exception ex)
        {
            if (ex.InnerException != null)
            {
                HttpContext.Current.Response.Write("内部错误信息:" + ex.InnerException.Message + "<br>");
            }
            req.Abort();
            throw ex;
        }
        return string.Empty;
    }

错误1:The requested security protocol is not supported. (不支持请求的安全协议)

原因:因为服务器上需安装.net4.5,(如果无效可以尝试重装IIS)

错误2:The remote name could not be resolved: 'nginx.platform.xxxxxx.cn'

原因:服务器无法解析这个域名,需要配置DNS解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值