Problems with WebClient + HTTPS

I am currently integrating with a third party system. This system requires me to send a request using XML / HTTPS. The third participant sent me a certificate and I installed it

I am using 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 connected connection was closed: Failed to establish trust for the SSL / TLS secure channel.

UPDATE Since this is the test server I'm working with, the certificate is not trusted and verification fails ... To get around this in the testing / debugging 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; } 

More here and here

+60
c # webclient
Feb 11 '09 at 11:15
source share
3 answers

The shortest code notation for all certificates is actually:

 ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 

And it works well for this error. Needless to say, you must provide an implementation that actually verifies the certificate and decides based on the certificate information if the message is safe. For testing purposes, use the line of code above.

+68
Apr 03 '11 at 23:29
source

For the version of the original VB.NET response, here you go (converters do not work well when you need to hook events using the AddressOf operator). 1st code preceding using the WebClient () or HttpWebRequest () object:

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

.. and wire 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 
+7
01 Oct '10 at 20:18
source

Try it, 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); } } 
-one
Apr 24 2018-12-12T00:
source



All Articles