1. 程式人生 > >java1.7訪問https Remote host closed connection during handshake

java1.7訪問https Remote host closed connection during handshake

在使用new DefaultHttpClient(ClientConnectionManager clientConnectionManager)之前對clientConnectionManager進行初始化改造。

示例程式碼如下(重新定義一個DefaultHttpClient的子類。使用時new一個新的例項然後執行init方法,也可將初始化方法放在每個具體執行方法中):

public class HttpsUtil extends DefaultHttpClient {

    public final static String HTTPGET = "GET";
    public final static String HTTPPUT = "PUT";
    public final static String HTTPPOST = "POST";
    public final static String HTTPDELETE = "DELETE";
    public final static String HTTPACCEPT = "Accept";
    public final static String CONTENT_LENGTH = "Content-Length";
    public final static String CHARSET_UTF8 = "UTF-8";
    private static HttpClient httpClient;

    public void init() throws Exception {
        String base_Path = "/Users/jinx/Documents/jobs/git_mine/base/base";// 存放證書資料夾位置 
        String selfcertpath = base_Path + "ca.jks";//自身cert
        String trustcapath = base_Path + "outgoing.CertwithKey.pkcs12"; //授信cert

        KeyStore selfCert = KeyStore.getInstance("pkcs12");
        selfCert.load(new FileInputStream(selfcertpath), Constant.SELFCERTPWD.toCharArray());
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");
        kmf.init(selfCert, Constant.SELFCERTPWD.toCharArray());
        KeyStore caCert = KeyStore.getInstance("jks");
        caCert.load(new FileInputStream(trustcapath), Constant.TRUSTCAPWD.toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");
        tmf.init(caCert);
        SSLContext sc = SSLContext.getInstance("TLSv1.2");//重點是這裡,JDK1.8使用TLS即可,jdk1.7使用TLSv1.2
sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); SSLSocketFactory ssf = new SSLSocketFactory(sc, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", "埠", ssf)); httpClient = new DefaultHttpClient(ccm); } }
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;