依赖:

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>3.10.0</version>
</dependency>

Example:

public class MQHttpClient {
/** * GET | POST | DELETE * * @param paramsMap if null and the request is Get,otherwise dependent on requestMethodEnum * @param paramsMap if the request is post,and you can put the params in the map otherwise to null * @param cookie if you need cookie to veriy,you can use it, otherwise to null * @return responseJsonString */public static String sendRequestURL(String url, HashMap<String, Object> paramsMap, RequestMethodEnum requestMethodEnum, String cookie) {    MediaType JSON = MediaType.parse("application/json; charset=utf-8");    RequestBody body = RequestBody.create(JSON, com.alibaba.fastjson.JSON.toJSONString(paramsMap, true));    Request.Builder request = new Request.Builder();    Response response;    try {        OkHttpClient client = new OkHttpClient.Builder().build();        OkHttpClient httpClient = trustAllSslClient(client);        // GET        if (paramsMap == null) {            if (cookie != null) {                response = httpClient.newCall(request.url(url).addHeader("cookie", cookie).get().build()).execute();            } else {                response = httpClient.newCall(request.url(url).get().build()).execute();            }        } else { // POST || DELETE            if (requestMethodEnum.getValue().equals(RequestMethodEnum.POST.getValue())) {                if (cookie != null) {                    response = httpClient.newCall((request.url(url).post(body)).addHeader("cookie", cookie).build()).execute();                } else {                    response = httpClient.newCall((request.url(url).post(body)).build()).execute();                }            } else {                if (cookie != null) {                    response = httpClient.newCall((request.url(url).post(body)).addHeader("cookie", cookie).build()).execute();                } else {                    response = httpClient.newCall((request.url(url).post(body)).build()).execute();                }            }        }        String responseBody = response.body().string();        logger.info("responseBody:" + responseBody + ", Code:" + response.code());        return responseBody;    } catch (Exception e) {        logger.info("get exception:" + e.toString());        e.printStackTrace();        return null;    }}

private static final TrustManager[] trustAllCerts = new TrustManager[]{        new X509TrustManager() {            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {                return new java.security.cert.X509Certificate[]{};            }        }};

private static final SSLContext trustAllSslContext;

static {    try {        trustAllSslContext = SSLContext.getInstance("SSL");        trustAllSslContext.init(null, trustAllCerts, new java.security.SecureRandom());    } catch (Exception e) {        throw new RuntimeException(e);    }}

private static final SSLSocketFactory trustAllSslSocketFactory = trustAllSslContext.getSocketFactory();

public static OkHttpClient trustAllSslClient(OkHttpClient client) {    OkHttpClient.Builder builder = client.newBuilder();    builder.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager) trustAllCerts[0]);    builder.hostnameVerifier(new HostnameVerifier() {        public boolean verify(String hostname, SSLSession session) {            return true;        }    });    return builder.build();}}

最新文章

  1. 如何为编程爱好者设计一款好玩的智能硬件(九)——LCD1602点阵字符型液晶显示模块驱动封装(下)
  2. yum install maven
  3. JavaScript实现XML与JSON互转代码(转载)
  4. poj 2786 - Keep the Customer Satisfied
  5. 在Tomcat中采用基于表单的安全验证
  6. exgcd证明和最基础应用
  7. react-router 4.0(一)
  8. Java笔试面试题整理第五波
  9. eclipse windowbuilder palette 空白
  10. 升级tensorflow1.0到1.3,报错ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory Failed to load the native TensorFlow runtime.
  11. Python 列表(List)操作方法详解
  12. Jenkins安装以及邮件配置
  13. can总线实现stm32的IAP
  14. node项目中用到的一些模块
  15. mysql性能测试--sysbench实践
  16. libconfig C++ 学习笔记
  17. 在对方电脑建立IPC连接, 利用IPC$入侵 运行木马
  18. Uva1025 A Spy in the Metro
  19. Could not connect to Redis at IP No route to host
  20. JavaScript 原型和引用趣点

热门文章

  1. k3s-初体验
  2. I NEED A OFFER! HDU - 1203
  3. R - Cow and Message CodeForces - 1307C
  4. test_HTMLTestRunnerCN.py
  5. 基于thinkphp3.2.3开发的CMS内容管理系统 - ThinkPHP框架
  6. Jmeter系列(5)- jmeter.properties常用配置项讲解
  7. GraphicsLab Project 之 Curl Noise
  8. Caused by:java.lang.ClassNotFoundException:org.apache.hadoop.yarn.util.Apps
  9. bootstrop日历
  10. [Qt] 默认的槽函数 例如 on_pushButton_clicked()