依赖:

<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();}}

HTTPS GET | POST | DELETE 请求的更多相关文章

  1. 浅谈get,post,put和delete请求

    get.put.post.delete含义与区别   1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影 ...

  2. 【转】AJAX发送 PUT和DELETE请求注意事项

    jax使用restful服务发送put 和 delete 请求时直接传参会出现问题 一,采用POST  + _method:delete/put  + filter 的方法ajax发送put 和 de ...

  3. Asp.Net Core IIS发布后PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词)

    一.在使用Asp.net WebAPI 或Asp.Net Core WebAPI 时 ,如果使用了Delete请求谓词,本地生产环境正常,线上发布环境报错. 服务器返回405,请求谓词无效. 二.问题 ...

  4. 让IIS7和IIS6识别PUT和DELETE请求

    项目组最近需要开展自动化测试,针对老的Aspx页面,这个做自动化测试的成本太高,于是我们想从老代码中封装一些ashx的Restful服务出来,Restful我们使用HTTP的GET.POST.PUT. ...

  5. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

  6. 部署Web API后Delete请求总是报 405(Method Not Allowed)解决办法

    WebDAV                   安装IIS的时候如果选择了WebDAV(Web Distribution Authorization Versioning) Publish,则所有的 ...

  7. 解决SpringMVC put,patch,delete请求数据拿不到的问题

    解决SpringMVC put,patch,delete请求参数拿不到的问题 废话不多说,核心代码如下: 在web.xml中添加如下代码 <!-- 解决web端不能put,delete等请求的问 ...

  8. .NetCore 网站DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法

    .netCore网站Delete请求405错误 解决方案 1:在启用或关闭Windows功能 –> Internet Information Services 关闭WebDAV发布(这种方式可能 ...

  9. 在ASP.NET Core中用HttpClient(二)——发送POST, PUT和DELETE请求

    在上一篇文章中,我们已经学习了如何在ASP.NET Core中使用HttpClient从Web API获取数据.此外,我们还学习了如何使用GetAsync方法和HttpRequestMessage类发 ...

随机推荐

  1. JS生成随机颜色(rgb)

    /*随机获取颜色*/ function getRandomColor() { var r = Math.floor(Math.random() * 256); var g = Math.floor(M ...

  2. 解决同层hover事件重叠闪烁问题

    完整代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. gdb 调试中No symbol “***” in current context解决方法

    主要是因为GCC/G++版本和GDB不匹配造成的,网上也有说是因为O2优化问题,具体啥原因需要自己尝试一下. 解决: 放狗搜索,解决办法是在编译是加-gdwarf-3即可,出现这样的原因是gcc,gd ...

  4. 腾讯推出超强少样本目标检测算法,公开千类少样本检测训练集FSOD | CVPR 2020

    论文提出了新的少样本目标检测算法,创新点包括Attention-RPN.多关系检测器以及对比训练策略,另外还构建了包含1000类的少样本检测数据集FSOD,在FSOD上训练得到的论文模型能够直接迁移到 ...

  5. Linux网络编程(1)

    Preview 课程要求,所以学了一下UNIX网络编程,老师说挺简单的,实际上手之后才发现这里面关系没那么简单.从CS:APP11章网络编程,再加上不停地man,对当前的学习做个总结,也顺带当个报告了 ...

  6. 改善 Python 程序的 91 个建议

    1.引论 建议1:理解Pythonic概念—-详见Python中的<Python之禅> 建议2:编写Pythonic代码 避免不规范代码,比如只用大小写区分变量.使用容易混淆的变量名.害怕 ...

  7. php中session_id()函数详细介绍,会话id生成过程及session id长度

    php中session_id()函数原型及说明session_id()函数说明:stringsession_id([string$id])session_id() 可以用来获取/设置 当前会话 ID. ...

  8. 百度云BCH配置说明

    百度云虚拟空间(BCH)  来源:https://www.cnblogs.com/llll/p/7930029.html 参考资料:https://cloud.baidu.com/doc/BCH/Ge ...

  9. webug3.0靶场渗透基础Day_2(完)

    第八关: 管理员每天晚上十点上线 这题我没看懂什么意思,网上搜索到就是用bp生成一个poc让管理员点击,最简单的CSRF,这里就不多讲了,网上的教程很多. 第九关: 能不能从我到百度那边去? 构造下面 ...

  10. 2019-2020-1 20199329《Linux内核原理与分析》第八周作业

    <Linux内核原理与分析>第八周作业 一.本周内容概述: 理解编译链接的过程和ELF可执行文件格式 编程练习动态链接库的两种使用方式 使用gdb跟踪分析一个execve系统调用内核处理函 ...