When you use httpwebrequest to access an SSL-type address https: // XXXX, the error "failed to establish a trust relationship for the SSL/TLS Security Channel (cocould not establish trust relationship for the SSL/TLS Secure Channel)"
I checked msdn and found a solution. For SSL websites, certificates must be provided during connection. If client certificates are not requiredReturns a security confirmation.You can. However, the implementation of this method is different in. NET 1.1 and. NET 2.0. Below are two implementation methods of the Framework Version:
Namespace used:
Using System . Net ; Using System . Net . Security ; Using System . Security . Authentication ; Using System . Security . Cryptography . X509certificates ;
. NET 2.0
Public Bool Checkvalidationresult ( Object Sender , X509certificate Certificate , X509chain chain , Sslpolicyerrors errors ){ // Directly confirm, otherwise it cannot be opened Return True ;} Private void Button#click ( Object Sender , Eventargs E ){ Servicepointmanager . Servercertificatevalidationcallback = New System . Net . Security . Remotecertificatevalidationcallback ( Checkvalidationresult ); Httpwebrequest REQ = ( Httpwebrequest ) Webrequest . Createdefault ( New Uri ( "Https://zu14.cn /" )); REQ . Method = "Get" ; Httpwebresponse Res = ( Httpwebresponse ) REQ . Getresponse (); //... It is in normal use. It is the same as accessing common http: // address.
. NET 1.1
Internal Class Acceptallcertificatepolicy: icertificatepolicy { Public Acceptallcertificatepolicy (){} Public Bool Checkvalidationresult ( Servicepoint spoint , System . Security . Cryptography . X509certificates . X509certificate Cert , Webrequest wrequest , Int Certprob ){ // Confirm directly Return True ;}} Private void Button#click ( Object Sender , Eventargs E ){ Servicepointmanager . Certificatepolicy = New Acceptallcertificatepolicy () ; Httpwebrequest REQ = ( Httpwebrequest ) Webrequest . Createdefault ( New Uri ( "Https://zu14.cn /" )); REQ . Method = "Get" ; Httpwebresponse Res = ( Httpwebresponse ) REQ . Getresponse (); //... It is in normal use. It is the same as accessing common http: // address.