How to Use webrequest to connect to an SSL website

Source: Internet
Author: User

I have searched many places and found the following answers on a foreign website to share with you:

Http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet/2006-03/msg02317.html

[Open this link in a new window]

// NET 1.0/1.1 version:
// ======================================

Using System. net;
Using System. Security. cryptography. x509certificates;

/**/ ///
///You can call this method before using webrequest to connect.
///
Private   Void Methodtoaccessssl ()
{
// 
Servicepointmanager. certificatepolicy= NewAcceptallcertificatepolicy ();

//Webrequest myrequest = webrequest. Create (URL );
}



Internal   Class Acceptallcertificatepolicy: icertificatepolicy
{
Public Acceptallcertificatepolicy () {}
Public   Bool Checkvalidationresult (servicepoint spoint,
X509certificate cert, webrequest wrequest, Int Certprob)
{
Return True;
}
}


// NET 2.0 version
// ======================

Using System. net;
Using System. Security. cryptography. x509certificates;
Using System. net. Security;

/**/ ///
///You can call this method before using webrequest to connect.
///
Private   Void Methodtoaccessssl ()
{
// 
Servicepointmanager. servercertificatevalidationcallback=
NewRemotecertificatevalidationcallback (validateservercertificate );
//Webrequest myrequest = webrequest. Create (URL );
}


// The following method is invoked by the remotecertificatevalidationdelegate.
Public   Static   Bool Validateservercertificate (
Object Sender,
X509certificate certificate,
X509chain chain,
Sslpolicyerrors)
{
If (Sslpolicyerrors = Sslpolicyerrors. None)
Return   True ;

Console. writeline ( " Certificate error: {0} " , Sslpolicyerrors );

// Do not allow this client to communicate with unauthenticated servers.
Return   False ;
}

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.