赞
踩
Http 请求 Https 的时候,会出现 请求被中止: 未能创建 SSL/TLS 安全通道的异常。
以下提供解决方法..
- public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- //直接确认,否则打不开
- return true;
- }
- // (HttpWebRequest)WebRequest.Create(url); 创建链接前,加入全部变量
- ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
- // 只有.net4.5以上才有Tls11,Tls12,建立通道
2. 设置请求的 UserAgent, UserAgent 为空也请求失败
例如:
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
- X509Certificate2 cert = new X509Certificate2(“证书物理路径”,“密码”,
- X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
- request.ClientCertificates.Add(cert);
如何对NetworkService进行授权呢?
安装授权工具
附下载地址:https://www.microsoft.com/en-us/download/details.aspx?id=19801
winhttpcertcfg -g -c LOCAL_MACHINE\MY -s "test" -a "NetworkService"
test是证书名,不带后缀,替换成你的证书名字就行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。