发送HTTPS请求

package com.suning.epp.trt.util.r32epp;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
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.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpsClientUtil {
    private static final String UTF_8 = "UTF-8";
    private HttpsClientUtil() {
    }
    private static final Logger log = LoggerFactory
            .getLogger(HttpsClientUtil.class);
    public static String post(List<BasicNameValuePair> params, String url,
            String logPrefix) {
 
        CloseableHttpClient httpClient = createSSLClientDefault(logPrefix);
        try {
            HttpPost post = createPost(params, url, logPrefix);
            HttpResponse response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity);
            return body;
        } catch (Exception e) {
            throw new EppRuntimeException(e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                log.error(logPrefix, e);
            }
        }
    }
    public static CloseableHttpClient createSSLClientDefault(String logPrefix) {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
                    nullnew TrustStrategy() {
                        // 信任所有
                        public boolean isTrusted(X509Certificate[] chain,
                                String authType) throws CertificateException {
                            return true;
                        }
                    }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (Exception e) {
            log.error(logPrefix, e);
        }
        return HttpClients.createDefault();
    }
    private static HttpPost createPost(List<BasicNameValuePair> params,
            String url, String logPrefix) throws Exception {
        HttpPost post = new HttpPost(url);
        HttpEntity httpEntity = new UrlEncodedFormEntity(params, UTF_8);
        post.setEntity(httpEntity);
        printParams(url, logPrefix, httpEntity);
        return post;
    }
    private static void printParams(String url, String logPrefix,
            HttpEntity httpEntity) throws IOException {
        BufferedInputStream bif = null;
        try {
            bif = new BufferedInputStream(httpEntity.getContent());
            byte[] byteArr = new byte[(int) httpEntity.getContentLength()];
            byte tmpByte[] = new byte[1024];
            int totalCounts = 0;
            int size = bif.read(tmpByte);
            while (size > 0) {
                System.arraycopy(tmpByte, 0, byteArr, totalCounts, size);
                totalCounts = totalCounts + size;
                size = bif.read(tmpByte);
            }
            log.info(logPrefix + "HTTP请求:{}?{}", url,
                    URLDecoder.decode(new String(byteArr, UTF_8), UTF_8));
        } catch (Exception e) {
            log.error(logPrefix, e);
        } finally {
            if (bif != null) {
                bif.close();
            }
        }
    }
}
posted @ 2017-09-18 12:32  江正军  阅读(2161)  评论(0编辑  收藏  举报