Closeablehttpclient Load certificate to access HTTPS Web site

Source: Internet
Author: User
Web sites that require security generally use HTTPS to encrypt requests and responses to transmissions. HTTPS can not be separated from the certificate, about the certificate is not said more. Apache's httpclient support HTTPS,

The following is an official sample program that uses the My.store file in the program.

This file is not a Web site certificate, but a certificate library containing your own password. This file needs to be generated by itself, and using the Keytool command in JDK makes it easy to generate my.store files. The steps are as follows (take Alipay for example):

Browser (for example, chrome) access to https://www.alipay.com/, click on the left side of the small lock, you can view Alipay's certificate information


The certificate information of the payment package is exported, the certificate format has many, der, CER and so on. Choose whatever you want.

command line or shell execution Keytool-import-alias "My Alipay cert"-file www.alipay.com.cert-keystore My.store,

If the Keytool command is not recognized, check to see if the JDK's environment variables are set correctly. "My Alipay cert" is an individual name, casually taken. The "Www.alipay.com.cert" file is the certificate of Alipay that was exported from the browser.

"My.store" is the build's own certificate library file. Carriage return execution, the effect is as follows:

OK, now you can execute the following code:

[Java]  View plain  copy package com.yeetrack.httpclient;         /**        * Created with IntelliJ IDEA.       *  User: victor       * Date: 13-10-11       *  Time:  pm 3:09       * to change this template use  File | Settings | File Templates.       */         import java.io.File;         import  java.io.fileinputstream;         import java.security.KeyStore;          import javax.net.ssl.SSLContext;          import org.apache.http.HttpEntity;         import org.apache.http.client.methods.closeablehttpresponse;         import  org.apache.http.client.methods.httpget;         import  org.apache.http.conn.ssl.sslcontexts;         import  org.apache.http.conn.ssl.sslconnectionsocketfactory;         import  org.apache.http.impl.client.closeablehttpclient;         import  org.apache.http.impl.client.httpclients;         import  org.apache.http.util.entityutils;               /**       *  code shows if you use Ssl context to create a secure socket connection        */          public class ClientCustomSSL {             public final staTic void main (String[] args)  throws Exception {        &nbs

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.