C#中的IIS,TLS和SSL版本或SSL HttpWebRequest“无法创建SSL / TLS安全通道

时间:2016-02-10 13:43:24

标签: ssl

有几件事,另一台服务器(Webmethods Integration Server)对它接受的协议类型有点挑剔。因此,在这里摆弄一些东西后,修复它的两件事情就是:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

req.ProtocolVersion = HttpVersion.Version10;

所以整个代码是:

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

**//for TLS Version as set TLSv1.2**

ServicePointManager.Expect100Continue = true;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://websiteURL:9000");

//Header Settings

req.Method = "POST"; // Post method

req.ContentType = "text/xml";// content type

req.KeepAlive = false;

req.ProtocolVersion = HttpVersion.Version10;

//Certificate with private key

X509Certificate2 cert = new X509Certificate2("Cert.der","Password");

req.ClientCertificates.Add(cert);

req.PreAuthenticate = true;

String XML = "Test Message"//reader.ReadToEnd();

byte[] buffer = Encoding.ASCII.GetBytes(XML);

req.ContentLength = buffer.Length;

// Wrap the request stream with a text-based writer

Stream writer = req.GetRequestStream();

// Write the XML text into the stream

writer.Write(buffer, 0, buffer.Length);

writer.Close();

WebResponse rsp = req.GetResponse();

StreamReader responseStream = new StreamReader(rsp.GetResponseStream());

1 个答案:

答案 0 :(得分:0)

//对于TLS版本,在服务电话之前设置TLSv1.2呼叫

ServicePointManager.Expect100Continue = true;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;