网络客户端 + HTTPS 问题

发布于 2024-07-13 07:44:50 字数 1268 浏览 7 评论 0原文

我目前正在与第三方创建的系统集成。 该系统要求我使用 XML/HTTPS 发送请求。 第 3 方向我发送了证书,我安装了它,

我使用以下代码:

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 安全通道的信任关系。

更新因为我正在使用它的测试服务器,所以证书不受信任并且验证失败...要在测试/调试环境中绕过此问题,请创建一个新的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;
}

阅读更多此处此处

I am currently integrating with a system created by a 3rd party. This system requires me to send a request using XML/HTTPS. The 3rd party send me the certificate and I installed it

I use the following code:

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

This code returns the following WebException:

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

UPDATE Because it's a test server I am working against, the certificate isn't trusted and validation fails... To bypass this in test/debug environment, create a new ServerCertificateValidationCallback

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

and here is my "fake" callback

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

Read more here and here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-07-20 07:44:50

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

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

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

The shortest notation of the code to allow all certificates is actually:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

And works well for this error. Needless to say that you should provide an implementation which actually checks the certificate and decides based on the certificate information if the communication is safe. For test purposes, use the above line of code.

二手情话 2024-07-20 07:44:50

对于原始答案的 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

For the VB.NET version of the original answer, here you go (converters don't work well when needing to wire up events with the 'AddressOf' operator). 1st code that goes before using a WebClient() or HttpWebRequest() object:

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

..and the wired up method code:

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
所有深爱都是秘密 2024-07-20 07:44:50

试试这个,它有效:

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

Try this, it works:

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); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文