Thursday, December 15, 2011

connect to web service with SLL settings

I got a task to explore an API to get some data to be displayed in a new interface. The problem is the web service is using SSL. Actually the first thing in my mind is using web reference from VS.net, so when I tried to add web reference, there is an error show up like this:

unable to download wsdl file

Tried many times but this error still occurs, then I search in google, and last thing I found is this site: http://debugguru.blogspot.com/2008/06/unable-to-download-wsdl-file-while.html
and indeed it solved the problem, wkwkwk.. Just close the solutions, and reopen, I tried to add web reference again, it works, lol.

But apparently I don't use the web reference. I try to connect to wsdl using Service Reference.
I try to connect, but this error occurs:

Could not establish trust relationship for the SSL/TLS secure channel with authority 'xx.xx.x.xx'.

I search over the net then I found this code:

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

public class TestUtils

{

public static void OverrideCertificateValidation()

{

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertValidate);

}

private static bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)

{

return true;

}

}


Hope it will be useful for you :)

-julie-

0 comments:

Post a Comment