请求被中止无法创建ssl / tls安全通道。 HttpWebRequest的

时间:2016-11-01 07:33:50

标签: ssl

我正在使用我在Google上找到的方法,但这并不像我希望的那样有效。

 string resultxml = string.Empty;
 #region First Link code
 try
 {
     ServicePointManager.Expect100Continue = true;
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

     //Header Settings
     req.Method = "POST"; // Post method
     req.ContentType = "text/xml";// content type
     req.KeepAlive = false;
     req.ProtocolVersion = HttpVersion.Version10;
     //Certificate with private key
     String certificateFileNameWithPath = "C:\\JPCertificates\\test.pfx";
     String certificatePassword = "password";
     X509Certificate2 cert = new X509Certificate2(certificateFileNameWithPath, certificatePassword);
     req.ClientCertificates.Add(cert);
     req.PreAuthenticate = true;
     String XML = 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());

     #endregion
 }
 catch (Exception ex)
 {
     throw ex;
 }
 return resultxml;

 //Send the SOAP message StreamWriter stm = new    StreamWriter(request.GetRequestStream(), Encoding.UTF8); 
stm.Write(requestXML); 
stm.Flush(); 
stm.Close(); 
//Send the SOAP message StreamWriter stm = new StreamWriter(request.GetRequestStream(), Encoding.UTF8); 
stm.Write(requestXML); 
stm.Flush(); 
stm.Close(); 

0 个答案:

没有答案