2012-09-07 41 views
9

Possible Duplicate:
The request was aborted: Could not create SSL/TLS secure channelYêu cầu đã bị hủy bỏ: Không thể tạo SSL/TLS kênh an toàn

Tôi cố gắng để gửi một yêu cầu http với một giấy chứng nhận phía khách hàng. Tệp, trong trường hợp này là tệp .p12. Tuy nhiên khi nó đạt đến dòng responseStream = httpRequest.GetRequestStream(); nó ném một WebException: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

Tôi đang gỡ lỗi này trên IIS7.5 (trên cửa sổ 7), nơi nhận dạng hồ bơi ứng dụng là "LocalSystem".

Làm cách nào để giải quyết vấn đề này?

 System.IO.Stream responseStream = null; 
     string errorString = string.Empty; 
     ; 
     string postData = string.Empty; 
     HttpWebRequest httpRequest = null; 
     System.Text.Encoding Encoding = new System.Text.UTF8Encoding(); 
     try 
     { 
      XmlDocument orderXml = new XmlDocument(); 
      orderXml.Load(@"c:\xmlfile.xml"); 
      postData = orderXml.InnerXml; 

      byte[] byte1 = Encoding.GetBytes(postData); 

      httpRequest = (HttpWebRequest)WebRequest.Create("https://testurl.com/SOAP_v1_0/"); 
      httpRequest.Method = "POST"; 
      httpRequest.Timeout = 9000; 
      httpRequest.KeepAlive = false; 
      httpRequest.ContentType = "text/xml; charset=" + "utf-8"; 
      httpRequest.ContentLength = byte1.Length; 

      X509Certificate2 certificate = new X509Certificate2(@"c:\file.p12", "password", X509KeyStorageFlags.Exportable); 
      X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 

      try 
      { 
       store.Open(OpenFlags.ReadWrite); 

       if (!store.Certificates.Contains(certificate)) 
       { 
        store.Add(certificate); 
       } 

       int indexOfCertificate = store.Certificates.IndexOf(certificate); 
       certificate = store.Certificates[indexOfCertificate]; 
      } 

      finally 
      { 
       store.Close(); 
      } 

      httpRequest.ClientCertificates.Add(certificate); 

      responseStream = httpRequest.GetRequestStream(); 

      responseStream.Write(byte1, 0, byte1.Length); 
     } 
     catch (WebException webExcp) 
     { 
      errorString += "Error message: " + webExcp.Message; 

      // Get the WebException status code. 
      WebExceptionStatus status = webExcp.Status; 

      if (status == WebExceptionStatus.ProtocolError) 
      { 
       // Get HttpWebResponse so that you can check the HTTP status code. 
       HttpWebResponse httpResponse = (HttpWebResponse)webExcp.Response; 
       errorString += "; The server returned protocol error " + httpResponse.StatusCode + " - " + httpResponse.StatusCode; 
       httpResponse.Close(); 
      } 
     } 
     catch (Exception e) 
     { 
      errorString += "Error message: " + e.Message; 
     } 
     finally 
     { 
      if (responseStream != null) 
      { 
       responseStream.Close(); 
      } 
     } 
    } 

Khi chạy với một dấu vết đăng nhập đó là những dòng băng việc xac định lỗi:

System.Net Information: 0 : [4968] SecureChannel#2399524 - Certificate is of type X509Certificate2 and contains the private key. 

System.Net Information: 0 : [4968] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc  = System.Net.SecureCredential) 

System.Net Error: 0 : [4968] AcquireCredentialsHandle() failed with error 0X8009030D. 

System.Net Information: 0 : [4968] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc  = System.Net.SecureCredential) 

System.Net Error: 0 : [4968] AcquireCredentialsHandle() failed with error 0X8009030D. 
System.Net.Sockets Verbose: 0 : [4968] Socket#59311937::Dispose() 

System.Net Error: 0 : [4968] Exception in the HttpWebRequest#50160154:: - The request was aborted: Could not create SSL/TLS secure channel. 

System.Net Error: 0 : [4968] Exception in the HttpWebRequest#50160154::EndGetRequestStream - The request was aborted: Could not create SSL/TLS secure channel. 
+0

Bạn có thể điều hướng thành công tới cùng một URL bằng trình duyệt không? Điều này có thành công trong việc thiết lập phiên SSL/TLS không? – Richard

+0

Không, tôi nhận được "Internet Explorer không thể hiển thị trang web" – Rutger

+0

Vì vậy, hãy xem chi tiết thông qua liên kết trên trang lỗi. Bạn cần phải biết tại sao không thể tạo kênh SSL/TLS. Nếu không có thông tin đó, chúng tôi dự đoán tại các cài đặt cấu hình trên máy khách và máy chủ. – Richard

Trả lời

31

Bạn cần phải tạo một bản ghi system.net cho ứng dụng của bạn. Bạn sẽ cần phải tạo một tập tin cấu hình myapp.exe.config và đặt như sau vào nó.

<?xml version="1.0" encoding="UTF-8"?> 

<configuration> 
    <system.diagnostics> 
     <trace autoflush="true" /> 
     <sources> 
      <source name="System.Net"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
      <source name="System.Net.Sockets"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
      <source name="System.Net.Cache"> 
       <listeners> 
        <add name="System.Net" /> 
       </listeners> 
      </source> 
     </sources> 
     <sharedListeners> 
      <add 
       name="System.Net" 
       type="System.Diagnostics.TextWriterTraceListener" 
       initializeData="System.Net.trace.log" 
      /> 
     </sharedListeners> 
     <switches> 
      <add name="System.Net" value="Verbose" /> 
      <add name="System.Net.Sockets" value="Verbose" /> 
      <add name="System.Net.Cache" value="Verbose" /> 
     </switches> 
    </system.diagnostics> 
</configuration> 

Nếu bạn chạy với tệp cấu hình này, nó sẽ tạo một tệp nhật ký được gọi là system.net.trace.log. Tệp đó sẽ có thêm chi tiết về lý do tại sao điều này không thành công.

+0

câu hỏi cập nhật với phần lỗi của nhật ký theo dõi – Rutger

+0

Nếu bạn không thêm thông báo lỗi chi tiết, hãy thêm 'traceOutputOptions =" ​​DateTime, ProcessId, ThreadId, Callstack "' làm thuộc tính vào 'systemList'' sharedListener'. Như thế này: ' ' – Ogglas

Các vấn đề liên quan