Get the server-side HTTPS certificate

Source: Internet
Author: User

Recently developed a requirement that involves obtaining a server-side HTTPS certificate. HTTPS calls are generally not very concerned about the underlying details, directly using WebClient or HttpWebRequest to send the request, both methods can not obtain the certificate information, need to use ServicePoint, this class is used to provide HTTP connection management.


Write a demo, take Sina homepage to try:

Using system;using system.net;using system.security.cryptography.x509certificates;namespace  GetServerCertificateDemo{    class Program    {         static void main (String[] args)          {            // Visit Sina home             var http = with WebClient  new webclient ();            var  Uri = new uri ("https://www.sina.com.cn");             http. Downloadstring (URI);                         //get servicepoint      by URI   &nbSp;    var servicepoint = servicepointmanager.findservicepoint (URI);                          //Fetch server certificate, x509certificate format, turn around              var servercert = new x509certificate2 (servicePoint.Certificate);             console.writeline ("issued to: {0}",  Servercert.subject);             console.writeline ( "Issuer: {0}",  servercert.issuer);             Console.WriteLine ("Serial number: {0}",  servercert.serialnumber);             console.writeline ("Fingers    stripes: {0}",  servercert.thumbprint);        &nbSp;    console.writeline ("Start    start: {0}",  servercert.notbefore);             console.writeline ("Over    period: {0}",  Servercert.notafter);         }    }}


Run to see the effect:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/A6/DA/wKioL1ncjAaQ57HSAACWpyaXpHQ820.png "title=" Untitled -5.png "alt=" Wkiol1ncjaaq57hsaacwpyaxphq820.png "/>


The upper part is the result of the program operation, the following is the server-side certificate information to be viewed in Firefox, the information can be corresponding. It doesn't matter if you have access to multiple different servers in your program, the key is to get servicepoint based on the URI, and then take the certificate that is the server.



This article is from the "Rabbit Nest" blog, please be sure to keep this source http://boytnt.blog.51cto.com/966121/1971227

Get the server-side HTTPS certificate

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.