2009-02-11 126 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); } 
}