c# - System.Net.WebException thrown when consuming a web service over HTTPS -


when making call web service running on server using https application throws system.net.webexception message "the underlying connection closed: not establish trust relationship remote server". i'm not sure how around issue , make call.

after research, found blog entry jan tielens explains going on , workaround problem:

when browse https site, dialog window asking if want trust certificate provided webserver. responsibility of accepting certificate handled user. let's webservice scenario, if want invoke webservice located on webserver uses ssl , https there problem. when make call code, there no dialog window popping up, , asking if trust certificate (luckily because pretty ugly in server-side scenarios); you'll following exception:

an unhandled exception of type system.net.webexception occurred in system.dll
additional information: underlying connection closed: not establish trust relationship remote server.

but there solution problem, can solve in code creating own certificatepolicy class (which implements icertificatepolicy interface). in class have write own checkvalidationresult function has return true or false, press yes or no in dialog window. development purposes i've created following class accepts certificates, won't nasty webexception anymore:

public class trustallcertificatepolicy : system.net.icertificatepolicy {     public trustallcertificatepolicy() { }      public bool checkvalidationresult(servicepoint sp, x509certificate cert, webrequest req, int problem)     {         return true;     } } 

as can see checkvalidationresult function returns true, certificates trusted. if want make class little bit more secure, can add additional checks using x509certificate parameter example. use certificatepolicy, you'll have tell servicepointmanager use it:

system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy(); 

this must done (one time during application life cycle) before making call webservice.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -