2013-09-11 35 views
3

我想在C#中執行HTTPS POST到沒有安裝正確的SSL證書(我的測試開發服務器)的服務器。該請求將使用WebClient以及HttpWebRequest超時。我已經建立了自己的ServerCertificateValidationCallback來繞過證書檢查,但這沒有幫助。如果我在網頁中進行完全相同的呼叫,則該呼叫成功。在C#HTTPS POST到服務器與證書不良 - 超時

所以:

我對Web客戶端後的代碼如下:

private static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) 
{ 
    //solve the problem of invalid certificates - accept all as valid 
    return true; 
}  

public void callPost(object o) 
{ 
    string myData = (string)o; 
    try 
    { 
     Uri uri = new Uri("https://testServer/myAction"); 
     WebClient client = new WebClient(); 
     client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
     byte[] uploadBytes = Encoding.UTF8.GetBytes(myData); 
     ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); 
     byte[] responseBytes = client.UploadData(uri, "POST", Encoding.UTF8.GetBytes(urlData)); 
     Console.WriteLine("WebRequest:{0}\n", Encoding.ASCII.GetString(responseBytes)); 
    } 
    catch (Exception e) 
    { 
     Console.WriteLine("WebRequest Error:{0}\n", e.ToString()); 
    } 
} 

任何想法如何得到這個職位在C#中的工作?謝謝!

+0

我不假設您可以將測試/ dev證書添加到您計算機的本地證書存儲中,以便它不再失效?或者,如果您在某個域上,請讓DC創建證書並將DC添加爲本地證書頒發機構? – Kye

+0

@Kye是對的。我試過這一次,但我沒有在我們的服務器的適當的權限。 –

回答

2

經過更多搜索,我發現有人遇到類似的問題,並找到了解決方案。 要調試此類和類似情況,請按照此處所述啓用SSL跟蹤/日誌記錄。 http://blogs.msdn.com/b/dgorti/archive/2005/09/18/471003.aspx

我的問題是,我需要如下

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

調用client.UploadData解決了這一問題之前添加這對我的代碼申報的SSL3連接。

相關問題