csdn_xuler
csdn_xuler
2017-12-27 11:50

c# .net 4.0 HttpWebRequest 访问https TLS1.2

300
  • tls1.2

用vs2010开发的web应用,部署在 IIS6.0 下,代码中用HttpWebRequest访问https TLS1.2网站,报protocolerror错误。
代码如下:
C# code
...
url="https://login.taobao.com/member/login.jhtml";
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.UserAgent = "Mozilla/5.0+(Windows+NT+5.1)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/55.0.2883.87+Safari/537.36";

request.GetResponse() as HttpWebResponse
...
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

{

return true; //总是接受

}

url="https://www.baidu.com" 或者 url="https://www.icbc.com.cn" 不会报错。

同样的代码升级到 vs2012,IIs8.0,.net4.5 还是报同样的错误。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

7条回答