How to handle insecure server certificates when httpwebrequest uses SSL connection

Source: Internet
Author: User
Tags ssl connection

Servicepointmanager. servercertificatevalidationcallback attributes
MS-help: // Ms. msdnqtr. v90.chs/fxref_system/html/dcd4157d-dba4-4b60-164a-9e433a045d3c.htm

Remotecertificatevalidationcallback delegate
MS-help: // Ms. msdnqtr. v90.chs/fxref_system/html/b3a0c706-1033-a543-01ca-23d09fcc121a.htm

Webrequest and SSL (the underlying connection was closed. cocould not establish trust relationship with remote server .)
Http://weblogs.asp.net/wim/archive/2004/04/02/106281.aspx

Call ssl vpn WebService in winform
Http://www.cnblogs.com/zhongzf/archive/2006/10/27/386902.html

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;

Using system. IO;
Using system. net;
Using system. collections;
Using system. Threading;

Using system. net. Security;
Using system. Security. cryptography. x509certificates;

Namespace getcwdata {
Public partial class form1: FORM {

Cookiecontainer cc;

Public form1 (){
Initializecomponent ();

Begingetdata ();
}

Private void begingetdata (){
This. Cc = new cookiecontainer ();

This.txt username. Text = getcwdata. properties. settings. Default. Username;
This.txt password. Text = getcwdata. properties. settings. Default. Password;
String url = getcwdata. properties. settings. Default. baseurl + getcwdata. properties. settings. Default. loginpage;

// Servicepointmanager. certificatepolicy = new acceptallcertificatepolicy ();
Servicepointmanager. servercertificatevalidationcallback = new remotecertificatevalidationcallback (validateservercertificate );

Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
Request. method = "Post ";
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. contentlength = 0;
Request. useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; sv1;. Net CLR 2.0.1124 )";
Request. cookiecontainer = This. CC;

Httpwebresponse response = (httpwebresponse) request. getresponse ();

Stream stream = response. getresponsestream ();
Streamreader reader = new streamreader (stream, encoding. utf8 );
String result = reader. readtoend ();

This.txt html. Text = result;
}

// Internal class acceptallcertificatepolicy: icertificatepolicy {
// Public acceptallcertificatepolicy (){
//}

// Public bool checkvalidationresult (servicepoint spoint, x509certificate cert, webrequest wrequest, int certprob ){
/// Always accept
// Return true;
//}
//}

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;

Return true;
}

}
}

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.