2012-09-07 46 views
9

Possible Duplicate:
The request was aborted: Could not create SSL/TLS secure channel請求已中止:無法創建

我試圖發送一個客戶端證書的http請求SSL/TLS安全通道。該文件在這種情況下爲 .p12文件。但是,當它到達線responseStream = httpRequest.GetRequestStream();它拋出一個WebException:System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

我調試IIS7.5(在Windows 7上),其中應用程序池標識爲「LocalSystem」。

我該如何解決這個問題?

 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(); 
      } 
     } 
    } 

當帶着一絲運行記錄這些都是specifing錯誤行:

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

你能用瀏覽器成功導航到相同的URL嗎?這是否成功建立了SSL/TLS會話? – Richard

+0

不,我得到「Internet Explorer無法顯示網頁」 – Rutger

+0

因此,請通過錯誤頁面上的鏈接查看詳細信息。您需要知道爲什麼無法創建SSL/TLS頻道。沒有這些信息,我們猜測客戶端和服務器上的配置設置。 – Richard

回答

31

你需要創建一個system.net日誌您的應用程序。您將需要創建一個myapp.exe.config配置文件,並將以下內容放入其中。

<?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> 

如果使用此配置文件運行,它將創建一個名爲system.net.trace.log的日誌文件。該文件將有更多的細節,爲什麼這是失敗。

+0

更新了跟蹤日誌的錯誤部分的問題 – Rutger

+0

如果您想更詳細的錯誤消息,請將'traceOutputOptions =「DateTime,ProcessId,ThreadId,Callstack」'作爲屬性添加到'sharedListener'' System.Net'。像這樣:'<添加 名稱= 「System.Net」 類型= 「System.Diagnostics.TextWriterTraceListener」 traceOutputOptions = 「日期時間,的ProcessID,的ThreadId,調用棧」 initializeData = 「System.Net.trace.log」 />' – Ogglas

相關問題