The code for creating a Web request and obtaining the request data is as follows:
WebRequest req = WebRequest. Create (url );
Req. Time out = 15000;
WebResponse result = req. GetResponse ();
Stream ReceiveStream = result. GetResponseStream ();
To add a certificate to the Code, use the HttpWebRequest object for the request. The Code is as follows:
// Add the callback method for the Verification Certificate
ServicePointManager. ServerCertificateValidationCallback = new RemoteCertificateValidationCallback (ValidateServerCertificate );
HttpWebRequest httpRequest = (HttpWebRequest) HttpWebRequest. Create (strQueryURL );
X509Certificate cerCaiShang = new X509Certificate (System. Web. HttpContext. Current. Server. MapPath (setting. PfxFile), setting. key );
HttpRequest. ClientCertificates. Add (cerCaiShang );
HttpWebResponse httpResponse = (HttpWebResponse) httpRequest. GetResponse ();
Stream receiveStream = httpResponse. GetResponseStream ();
// Callback Method
Public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
If (sslPolicyErrors = SslPolicyErrors. None)
Return true;
Return false;
}
This is done, but it always fails, the cause is unknown, and it is related to the evil Windows, also need to the client certificate + private key (pfx or p12 file ), to import the certificate to the relevant certificate area of the Operating System (windows), follow these steps:
For Windows xp/2003
1. Click Start, click Run, type mmc, and click OK.
2. Click Add/delete snap-in on the File menu.
3. In the Add/delete snap-in dialog box, click Add.
4. In the add independent unit dialog box, click certificate, and then click Add.
5. In the Certificate Management Unit dialog box, click computer account, and then click Next
6. In the select computer dialog box, click Finish.
7. Click Close In the add independent unit dialog box, and then click OK.
8. Expand certificate (Local Computer), expand individual, and then click certificate.
9. Right-click and choose "all tasks"> "import" to import your certificate.
Windows 7
1. Click Start, click Run, type mmc, and click OK.
2. Click Add/delete snap-in on the File menu.
3. Select a certificate from the list of available management units and click Add.
4. In the Certificate Management dialog box, select a computer account and click Next.
5. In the select computer dialog box, click Finish.
6. In the add or delete Management Unit dialog box, click OK.
7. Expand certificate (Local Computer), expand individual, and then click certificate.
8. Right-click and choose "all tasks"> "import" to import your certificate.
Test again to use it.
Reference: http://blog.wsdd.org /? P = 510