C #. VB. NET using httpwebrequest to access the HTTPS address (SSL)

Source: Internet
Author: User

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.   

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.