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