首页 新闻 会员 周边 捐助

c# HttpWebRequest ,无法创建SSL / TLS安全通道

0
悬赏园豆:20 [已解决问题] 解决于 2022-08-18 10:32

HttpWebRequest ,无法创建SSL / TLS安全通道,在win7系统上不行,在win10可以

已经添加了ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;也不行

电脑也安装了.net framework 4.6,为什么还会提示无法创建SSL / TLS安全通道?

//添加了下面的代码也不行
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

c#
问题补充:

win7 32位系统sp1,浏览器可访问tls1.2的网站,
安装了.net frawork4.6,
注册文件添加了tls1.2,
IISCrypto.exe也使用了,
启用了ssl密码套件顺序,
代码访问失败,
最后办法:电脑换了win10系统

HYRUI的主页 HYRUI | 初学一级 | 园豆:18
提问于:2022-08-12 13:34
< >
分享
最佳答案
0

试试这样,之前台湾老兄教我的:

        private static void EnableSSL()
        {
            var logger = LogManager.GetCurrentClassLogger();
            try
            { //try TLS 1.3
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)12288
                                                     | (SecurityProtocolType)3072
                                                     | (SecurityProtocolType)768
                                                     | SecurityProtocolType.Tls;
                logger.Info("TLS 1.3");
                
            }
            catch (NotSupportedException)
            {
                try
                { //try TLS 1.2
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072
                                                         | (SecurityProtocolType)768
                                                         | SecurityProtocolType.Tls;
                    logger.Info("TLS 1.2");
                }
                catch (NotSupportedException)
                {
                    try
                    { //try TLS 1.1
                        ServicePointManager.SecurityProtocol = (SecurityProtocolType)768
                                                             | SecurityProtocolType.Tls;
                        logger.Info("TLS 1.1");
                    }
                    catch (NotSupportedException)
                    { //TLS 1.0
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                        logger.Info("TLS 1.0");
                    }
                }
            }
        }

最好确认一下服务器的TLS版本是不是较高。
可参考下我之前的提问:https://q.cnblogs.com/q/134234/

收获园豆:20
会长 | 专家六级 |园豆:12463 | 2022-08-15 16:32

我电脑win10的,是可以访问的,用的就是tls1.2,
我试试

HYRUI | 园豆:18 (初学一级) | 2022-08-15 16:53

您好,服务器用的是tls1.2,可是win7的电脑我在注册表里加了tls1.2,还是不能用,为什么?

HYRUI | 园豆:18 (初学一级) | 2022-08-16 10:28

@HYRUI: 是不是framework版本低,你们用的什么版本

会长 | 园豆:12463 (专家六级) | 2022-08-16 17:15

@会长: 4.6的,会不会是电脑系统的问题?

HYRUI | 园豆:18 (初学一级) | 2022-08-17 09:11

@HYRUI: 有可能,和操作系统或framwork版本有关,要不你在别的win7电脑上试试

会长 | 园豆:12463 (专家六级) | 2022-08-17 09:18

@会长: 主要我在俩win7电脑都按照网上所有的办法都试了,就特别神奇,其中一台把ssl密码套件顺序启用后试了一下,能访问,再试之后一次也访问不上,另一台就一直不行

HYRUI | 园豆:18 (初学一级) | 2022-08-17 09:32
其他回答(1)
0

贴全部代码,我来看看

simadi | 园豆:134 (初学一级) | 2022-08-15 15:50

ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//| SecurityProtocolType.Tls
//| SecurityProtocolType.Tls12 //Tls11
//| SecurityProtocolType.Tls11;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
string url =//地址
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.Host = "";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36";

        request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
        request.Headers.Add("Accept-Language", "zh-CN,zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6");
        //ServicePointManager.Expect100Continue = true;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader read = new StreamReader(stream);
        string text = read.ReadToEnd();
        read.Close();
        stream.Close();
支持(0) 反对(0) HYRUI | 园豆:18 (初学一级) | 2022-08-15 15:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册