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 implementsicertificatepolicy
interface). in class have write owncheckvalidationresult
function has returntrue
orfalse
, press yes or no in dialog window. development purposes i've created following class accepts certificates, won't nastywebexception
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 usingx509certificate
parameter example. usecertificatepolicy
, you'll have tellservicepointmanager
use it:
system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy();
this must done (one time during application life cycle) before making call webservice.
Comments
Post a Comment