2009-02-11 64 views
61

我目前正在與由第三方創建的系統集成。該系統要求我使用XML/HTTPS發送請求。第三方寄給我的證書,我安裝了它WebClient + HTTPS問題

我使用下面的代碼:

using (WebClient client = new WebClient()) 
{ 
    client.Headers.Add(HttpRequestHeader.ContentType, "text/xml"); 

    System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); 
    var response = client.UploadData(address, "POST", encoding.GetBytes(msg)); 
} 

此代碼返回以下WebException

基礎連接已關閉:無法建立信任關係爲SSL/TLS安全通道。

UPDATE因爲這是我對工作的測試服務器,該證書不被信任和驗證失敗...要在測試/調試環境繞過這一點,創建一個新的ServerCertificateValidationCallback

ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(bypassAllCertificateStuff); 

這裏是我的 「假」 回調

private static bool bypassAllCertificateStuff(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) 
{ 
    return true; 
} 

更多herehere

+4

+1與您使用的代碼更新。因爲這個,很好的快速修復。 – 2010-08-31 20:06:37

+0

調試SSL Web服務時,這非常有用,而不是將提琴手CA根註冊到我的開發機器中!我只是在添加虛擬回調的部分放置了一個#if DEBUG,以便不將它放入生產代碼中。 – jishi 2010-11-15 13:02:28

+0

Go [here。](http://forums.asp.net/p/1174025/1972251.aspx) – Lonzo 2009-02-11 11:24:11

回答

8

對於原始答案的VB.NET版本,在這裏你去(當需要使用'AddressOf'操作符連接事件時轉換器不能很好地工作)。第一代碼使用Web客戶端(在此之前,雲)或HttpWebRequest的()對象:

ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf bypassAllCertificateStuff) 

..和有線向上方法的代碼:

Private Shared Function bypassAllCertificateStuff(ByVal sender As Object, ByVal cert As X509Certificate, ByVal chain As X509Chain, ByVal [error] As System.Net.Security.SslPolicyErrors) As Boolean 
    Return True 
End Function 
68

代碼的最短標記,以允許所有的證書是實際上:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

並且適用於此錯誤。不用說,你應該提供一個實際檢查證書的實現,並根據證書信息來決定通信是否安全。出於測試目的,請使用上面的代碼行。

-1

試試這個,它的工作原理:

class Ejemplo 
{ 
    static void Main(string[] args) 
    { 
     string _response = null; 
     string _auth = "Basic"; 
     Uri _uri = new Uri(@"http://api.olr.com/Service.svc"); 

     string addres = @"http://api.olr.com/Service.svc"; 
     string proxy = @"http://xx.xx.xx.xx:xxxx"; 
     string user = @"platinum"; 
     string pass = @"01CFE4BF-11BA"; 


     NetworkCredential net = new NetworkCredential(user, pass); 
     CredentialCache _cc = new CredentialCache(); 

     WebCustom page = new WebCustom(addres, proxy); 
     page.connectProxy(); 

     _cc.Add(_uri, _auth, net); 

     page.myWebClient.Credentials = _cc; 

     Console.WriteLine(page.copyWeb()); 
    } 

} 

public class WebCustom 
{ 
     private string proxy; 
     private string url; 
     public WebClient myWebClient; 
     public WebProxy proxyObj; 
     public string webPageData; 


     public WebCustom(string _url, string _proxy) 
     { 
      url = _url; 
      proxy = _proxy; 
      myWebClient = new WebClient(); 
     } 

     public void connectProxy() 
     { 
      proxyObj = new WebProxy(proxy, true); 
      proxyObj.Credentials = CredentialCache.DefaultCredentials; 
      myWebClient.Proxy = proxyObj; 
     } 

     public string copyWeb() 
     { return webPageData = myWebClient.DownloadString(url); } 
}