WebClient + HTTPS问题

时间:2009-02-11 11:15:40

标签: c# https webclient

我目前正在与第三方创建的系统集成。该系统要求我使用XML / 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

3 个答案:

答案 0 :(得分:70)

允许所有证书的代码的最短表示法实际上是:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

并且适用于此错误。不用说,您应该提供实际检查证书的实现,并根据证书信息决定通信是否安全。出于测试目的,请使用上面的代码行。

答案 1 :(得分:8)

对于原始答案的VB.NET版本,这里你去(当需要使用'AddressOf'运算符连接事件时,转换器不能正常工作)。使用WebClient()或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

答案 2 :(得分:-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); }
}