Java to obtain the https website certificate, with the call of https: webservice interface, httpswebservice

Source: Internet
Author: User

Java to obtain the https website certificate, with the call of https: webservice interface, httpswebservice

1. Obtain the https website certificate using java:

1. Create a java project, create an InstallCert class, and copy the following code

Package com; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStream; import java. io. inputStreamReader; import java. io. outputStream; import java. security. keyStore; import java. security. messageDigest; import java. security. cert. certificateException; import java. security. cert. x509Certificate; import javax.net. ssl. SSLContext; im Port javax.net. ssl. SSLException; import javax.net. ssl. SSLSocket; import javax.net. ssl. SSLSocketFactory; import javax.net. ssl. trustManager; import javax.net. ssl. trustManagerFactory; import javax.net. ssl. x509TrustManager;/*** obtain the certificate required by java from the website and pass in the domain name during the call. */Public class InstallCert {public static void main (String [] args) throws Exception {String host; int port; char [] passphrase; if (args. length = 1) | (args. length = 2) {String [] c = args [0]. split (":"); host = c [0]; port = (c. length = 1 )? 443: Integer. parseInt (c [1]); String p = (args. length = 1 )? "Changeit": args [1]; passphrase = p. toCharArray ();} else {System. out. println ("Usage: java InstallCert InstallCert. java

2. Run the main method as follows:

  

Note: Pay attention to changing the parameter class here. Do not make a mistake.

3. After you click "run", the console reports an error. Do not worry:

Enter 1 in the console and press Enter:

4. The certificate file jssecacerts will be generated under the project directory.

================================The above is the entire process of obtaining the https website certificate. ======================================

 

2. Call the https webservice interface. If the certificate is not registered, an error is returned. Follow these steps to register a certificate:

1. Add a static code block to the class that needs to call the webservice interface:

/** Set the certificate. */Static {javax.net. ssl. httpsURLConnection. setDefaultHostnameVerifier (new javax.net. ssl. hostnameVerifier () {public boolean verify (String hostname, javax.net. ssl. SSLSession sslSession) {// domain name or IP address if (hostname. equals ("www.baidu.com") {return true;} return false ;}}); // The second parameter is the certificate path System. setProperty ("javax.net. ssl. trustStore "," C: \ Program Files \ Java \ jdk1.7.0 _ 45 \ jre \ lib \ security \ jssecacerts "); System. setProperty ("javax.net. ssl. trustStorePassword "," changeit ");}
View Code

 

2. Change the domain name to the domain name for obtaining the certificate. Here is www.baidu.com.

3. Set the certificate path. Here, I copied the obtained certificate to the jdk security directory. This directory is the directory where jdk certificates are stored. We recommend that you store certificates in this directory.

4. Complete. Now you can call the https webservice interface.

Note: If the error still persists after the preceding steps, check the following:

1. Is the certificate path correct;

2. The domain name or ip address entered when obtaining the certificate is the domain name or ip address of the webservice interface to be called;

3. Does the domain name or ip address in the static code block correspond to the domain name or ip address used to obtain the 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.