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请求的更多相关文章

  1. 【转载】JMeter学习(三十六)发送HTTPS请求

    Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程中需要进行登录操作,而登录请求是HTTPS协议.这就需要对jmeter进行设置. ...

  2. Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

    使用WebClient 发送https请求 使用WebClient发送请求时,并且是以https协议: WebClient webClient = new WebClient(); string re ...

  3. 使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL

    这里使用的是HttpComponents-Client-4.1.2 package com.jadyer.util; import java.io.File; import java.io.FileI ...

  4. JMeter学习(三十六)发送HTTPS请求(转载)

    转载自 http://www.cnblogs.com/yangxia-test Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程 ...

  5. 【传输协议】发送https请求,由于客户端jdk版本过高,服务端版本低。导致异常:javax.net.ssl.SSLHandshakeException: Server chose SSLv3, but that protocol version is not enabled or not supported by the client.

    本地环境jdk为1.8,服务器使用jdk版本未知.但发送https请求,抛出如下异常,解决方案. 一:发送异常内容如下 javax.net.ssl.SSLHandshakeException: Ser ...

  6. python2/3 发送https请求时,告警关闭方法

    问题: 使用Python3 requests发送HTTPS请求,已经关闭认证(verify=False)情况下,控制台会输出以下错误: InsecureRequestWarning: Unverifi ...

  7. requests发送HTTPS请求(处理SSL证书验证)

    1.SSL是什么,为什么发送HTTPS请求时需要证书验证? 1.1 SSL:安全套接字层.是为了解决HTTP协议是明文,避免传输的数据被窃取,篡改,劫持等. 1.2 TSL:Transport Lay ...

  8. .Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2

    .Net Core 发送https请求 .net core 调用数字证书 使用X509Certificate2 .NET下面的 .netfromwork使用和asp.net core下使用方式不一样 ...

  9. 简单粗暴套娃模式组json发送https请求

    各位童鞋大家好,向来简单粗暴的铁柱兄给大家来玩一手套娃模式来组Json数据,不说别的,无脑套. 当然,这一手比较适合临场用一下,若长期用的话建议搞一套适用的框架,只管set就好了.话不多说开始上课. ...

  10. Python常见问题 - python3 使用requests发送HTTPS请求报certificate verify failed 错误

    当你使用 requests 发送HTTPS请求时 requests.get(url, parmas=parmas, headers=header, cookies=cookie) 出现了以下错误 HT ...

随机推荐

  1. HTML中鼠标滚轮事件onmousewheel

    IE/Opera属于同一类型,使用attachEvent即可添加滚轮事件. /*IE注册事件*/ if(document.attachEvent){ document.attachEvent('onm ...

  2. UOJ#30/Codeforces 487E Tourists 点双连通分量,Tarjan,圆方树,树链剖分,线段树

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ30.html 题目传送门 - UOJ#30 题意 uoj写的很简洁.清晰,这里就不抄一遍了. 题解 首先建 ...

  3. 51Nod1773 A国的贸易 多项式 FWT

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1773.html 题目传送门 - 51Nod1773 题意 给定一个长度为 $2^n$ 的序列,第 $ ...

  4. 1301 邻值查找(set 平衡树 | 链表)

    描述 给定一个长度为 n 的序列 A,A 中的数各不相同.对于 A 中的每一个数 A_i,求: min(1≤j<i) ⁡|A_i-A_j| 以及令上式取到最小值的 j(记为 P_i).若最小值点 ...

  5. RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—译文)

    part of Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616 Fielding, et al. 14 头字段规定 该章节定义了HTTP1.1标准所包 ...

  6. 将ant Design本地化,可通过link以及script直接引入html中使用

    一直想着能本地化antd的,不用npm以及dva那么复杂的配置环境来开发,并且本地化以后对以后链接flask的模板渲染机制也能很好的结合.下面是具体的实现方法: 1.将react的相关链接引入: &l ...

  7. C# ImageHelper

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; ...

  8. AE插入音乐

    将音乐文件(如MP3文件)直接拖拽到工程里就可以. 然后添加到合成里. 点击内容预览 ,就自动播放了. 在合成窗口里面,我们可以对于音频文件进行拖动,以及裁剪等操作,但是需要注意的是AE里面不能预览声 ...

  9. 在UnrealEngine中用Custom节点实现径向模糊

    //input NotUse 为了开启SceneTextureLookup函数而连接的节点,但是不参与逻辑 //input UV 屏幕缓存的坐标坐标 //input Strength 力度 //inp ...

  10. [mongoDB]PyMongo Cursor Not Found Error

    Python跑一个aggregate脚本,报错:pymongo.errors.CursorNotFound: Cursor not found, cursor id: 35411720832 搜了下原 ...