C # create an https request and use a pfx Certificate

Source: Internet
Author: User

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

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.