分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

发送HTTPS请求

发布时间:2023-09-06 01:12责任编辑:沈小雨关键词:暂无标签
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(
                    null, new 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();
            }
        }
    }
}

发送HTTPS请求

原文地址:http://www.cnblogs.com/jiangzhengjun/p/7542476.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved