您的位置:首页 > 理论基础 > 计算机网络

HttpClient 4.3.x Https TLS

2016-07-22 13:35 323 查看
import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.security.KeyManagementException;

import java.security.KeyStore;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.UnrecoverableKeyException;

import java.security.cert.CertificateException;

import java.util.HashMap;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import javax.net.ssl.KeyManagerFactory;

import javax.net.ssl.SSLContext;

import javax.net.ssl.TrustManager;

import org.apache.commons.io.IOUtils;

import org.apache.http.Consts;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.conn.scheme.Scheme;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.conn.ssl.SSLContextBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

public class HttpsClient

{

  private String sslKeyStorePath;

  private String sslKeyStorePassword;

  private HttpClient httpClient;

  public static final String SSLKEYSTORETYPE_PKCS12 = "PKCS12";

  public static final String SSLKEYSTORETYPE_JKS = "jks";

  public static final String SCHEME_HTTPS = "https";

  public static final int HTTPS_PORT = 8443;

  public static final String SSLCONTEXT_SSL = "TLS";

  public static final String KEYMANAGERFACTORY_SUNX509 = "sunx509";

  public HttpsClient(String sslKeyStorePath, String sslKeyStorePassword)

    throws KeyManagementException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException

  {

    this.sslKeyStorePath = sslKeyStorePath;

    this.sslKeyStorePassword = sslKeyStorePassword;

    this.httpClient = getHttpClient(getSSLContext());

  }

  public SSLContext getSSLContext()

    throws KeyManagementException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException

  {

    SSLContext sslContext = null;

    KeyStore kstore = KeyStore.getInstance("PKCS12");

    kstore.load(new FileInputStream(this.sslKeyStorePath), this.sslKeyStorePassword.toCharArray());

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance("sunx509");

    keyFactory.init(kstore, this.sslKeyStorePassword.toCharArray());

    TrustManager[] tm = { new MyX509TrustManager() };

    sslContext = SSLContext.getInstance("TLS");

    sslContext.init(keyFactory.getKeyManagers(), tm, null);

    return sslContext;

  }

  public HttpClient getHttpClient(SSLContext sslContext) {

    CloseableHttpClient httpClient = null;

    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);

    httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();

    return httpClient;

  }

  public String request(String url, HashMap formParams) throws ClientProtocolException, IOException

  {

    List valuePairs = new LinkedList();

    Iterator iter = formParams.entrySet().iterator();

    while (iter.hasNext()) {

      Map.Entry entry = (Map.Entry)iter.next();

      String key = (String)entry.getKey();

      String val = (String)entry.getValue();

      valuePairs.add(new BasicNameValuePair(key, val));

    }

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairs, Consts.UTF_8);

    HttpPost post = new HttpPost(url);

    post.setEntity(entity);

    HttpResponse httpResponse = this.httpClient.execute(post);

    return IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8);

  }

  public String getSslKeyStorePath()

  {

    return this.sslKeyStorePath;

  }

  public void setSslKeyStorePath(String sslKeyStorePath) {

    this.sslKeyStorePath = sslKeyStorePath;

  }

  public String getSslKeyStorePassword() {

    return this.sslKeyStorePassword;

  }

  public void setSslKeyStorePassword(String sslKeyStorePassword) {

    this.sslKeyStorePassword = sslKeyStorePassword;

  }

}

4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: