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. Linux Centos7安装最新anslib

    一.添加最新epel源 yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 二.添加最 ...

  2. ActiveMQ在windows下启动失败解决方案

    activemq.xml文件中的 <transportConnectors> <!-- DOS protection, limit concurrent connections to ...

  3. Codeforces 1036C Classy Numbers 【DFS】

    <题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R]  (1≤L≤R≤1018).,问你这个区间内有多 ...

  4. datatables跳转自定义页面(后端分页)

    在后端分页的情况下,怎么做到跳转自定义页面? 0x01 难点: 一. 怎么添加自定义代码? 前提:datatables在整个html加载完毕后,进行datatables数据的渲染,并且把右下角的 “上 ...

  5. Redis自学笔记:3.3入门-散列类型

    3.3散列类型 3.3.1介绍 散列类型不能嵌套其他数据类型,一个散列类型可以包含至多232-1个字段 散列类型适合存储对象:使用对象类别和ID构成键名,使用字段表示对象的数据, 而字段值则存储属性值 ...

  6. IDEA安装使用Lombok插件

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然IDEA开发环境下都有自动生成的快捷方式,但自动生成这些代码 ...

  7. Scrapy基础(十)———同步机制将Item中的数据写在Mysql

      前面讲解到将Item中的所有字段都已经填写完成,那么接下来就是将他们存储到mysql数据库中,那就用到了pipeline项目管道了:  对项目管道的理解:做一个比喻,爬取好比是开采石油,Item装 ...

  8. CodeForce VKcup A

    题目描述:例如A-B,B-C是好朋友,那么A-C一定是好朋友,给一些点,和一些描述,观察是否成立 题目链接:点我 一个互相认识的团体,一定是每个点都和其他点相连的,那么边数为n(n-1)/2,把得到的 ...

  9. [模板][P4782]2-SAT

    Description: 有n个布尔变量\(x_1\)~\(x_n\),另有m个需要满足的条件,每个条件的形式都是"\(x_i\)为true/false或\(x_j\)为true/false ...

  10. BZOJ4910 : [Sdoi2017] 苹果树

    问题等价于树形依赖背包,允许一条链每个点各免费一次. 设$f[i][j]$表示按DFS序考虑到$i$,体积为$j$的最大收益. 先放入不能免费的物品,等遍历完儿子后再放入必选的物品,那么$i$到根路径 ...