httpclient4.x处理https协议请求

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
 
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class HttpsSSLClient {
 
    /**
     * 获取Https 请求客户端
     * @return
     */
    public static CloseableHttpClient createSSLInsecureClient() {
        SSLContext sslcontext = createSSLContext();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new HostnameVerifier() {
 
            @Override
            public boolean verify(String paramString, SSLSession paramSSLSession) {
                return true;
            }
        });
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        return httpclient;
    }
 
    /**
     * 获取初始化SslContext
     * @return
     */
    private static SSLContext createSSLContext() {
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        return sslcontext;
    }
 
    /**
     * 自定义静态私有类
     */
    private static class TrustAnyTrustManager implements X509TrustManager {
 
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
 
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
 
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }
 
}


public static String doGet(String url, String charSet) throws Exception {
        // When an instance CloseableHttpClient is no longer needed and is about to go out
        // of scope the connection manager associated with it must be shut down by calling the
        // CloseableHttpClient.close() method.
        if (StringUtils.isBlank(url)) {
            throw new Exception("The request url is blank.");
        }
        CloseableHttpClient httpClient = null;
        if (StringUtils.startsWith(url, "https")) {
            httpClient = HttpsSSLClient.createSSLInsecureClient();
        } else {
            httpClient = HttpClients.createDefault();
        }
        HttpGet getMethod = new HttpGet(url);
        return doGet(url, charSet, httpClient, getMethod);
    }
 
 
 
 
private static String doGet(String url, String charSet, CloseableHttpClient httpClient, HttpGet getMethod)
            throws Exception {
        charSet = StringUtils.isBlank(charSet) ? "UTF-8" : charSet;
        CloseableHttpResponse response = null;
        HttpEntity responseEntity = null;
        try {
            response = httpClient.execute(getMethod);
            StatusLine status = response.getStatusLine();
            int state = status.getStatusCode();
            if (state != HttpStatus.SC_OK) {
                throw new Exception("响应编码:"+state);
            }
            responseEntity = response.getEntity();
            return EntityUtils.toString(responseEntity, charSet);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                EntityUtils.consume(responseEntity);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值