HTTPS GET | POST | DELETE 请求
依赖:
<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 请求的更多相关文章
- 浅谈get,post,put和delete请求
get.put.post.delete含义与区别 1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影 ...
- 【转】AJAX发送 PUT和DELETE请求注意事项
jax使用restful服务发送put 和 delete 请求时直接传参会出现问题 一,采用POST + _method:delete/put + filter 的方法ajax发送put 和 de ...
- Asp.Net Core IIS发布后PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词)
一.在使用Asp.net WebAPI 或Asp.Net Core WebAPI 时 ,如果使用了Delete请求谓词,本地生产环境正常,线上发布环境报错. 服务器返回405,请求谓词无效. 二.问题 ...
- 让IIS7和IIS6识别PUT和DELETE请求
项目组最近需要开展自动化测试,针对老的Aspx页面,这个做自动化测试的成本太高,于是我们想从老代码中封装一些ashx的Restful服务出来,Restful我们使用HTTP的GET.POST.PUT. ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
- 部署Web API后Delete请求总是报 405(Method Not Allowed)解决办法
WebDAV 安装IIS的时候如果选择了WebDAV(Web Distribution Authorization Versioning) Publish,则所有的 ...
- 解决SpringMVC put,patch,delete请求数据拿不到的问题
解决SpringMVC put,patch,delete请求参数拿不到的问题 废话不多说,核心代码如下: 在web.xml中添加如下代码 <!-- 解决web端不能put,delete等请求的问 ...
- .NetCore 网站DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法
.netCore网站Delete请求405错误 解决方案 1:在启用或关闭Windows功能 –> Internet Information Services 关闭WebDAV发布(这种方式可能 ...
- 在ASP.NET Core中用HttpClient(二)——发送POST, PUT和DELETE请求
在上一篇文章中,我们已经学习了如何在ASP.NET Core中使用HttpClient从Web API获取数据.此外,我们还学习了如何使用GetAsync方法和HttpRequestMessage类发 ...
随机推荐
- python干货:5种反扒机制的解决方法
前言 反爬虫是网站为了维护自己的核心安全而采取的抑制爬虫的手段,反爬虫的手段有很多种,一般情况下除了百度等网站,反扒机制会常常更新以外.为了保持网站运行的高效,网站采取的反扒机制并不是太多,今天分享几 ...
- 遇到自己喜欢的视频无法下载,python帮你解决
问题描述 python是一种非常好用的爬虫工具.对于大多数的爬虫小白来说,python是更加简洁,高效的代码.今天就用实际案例讲解如何爬取动态的网站视频. 环境配置:python3:爬虫库reques ...
- Pytorch实现MNIST手写数字识别
Pytorch是热门的深度学习框架之一,通过经典的MNIST 数据集进行快速的pytorch入门. 导入库 from torchvision.datasets import MNIST from to ...
- 13.create-react-app 构建的项目使用代理 proxy
1. 正常运行 npm run eject 2. create-react-app 的版本在低于 2.0 的时候可以在 package.json 增加 proxy 配置, 配置成如下: "p ...
- v&n赛 内存取证题解(已更新)
题目是一个raw的镜像文件 用volatility搜索一下进程 有正常的notepad,msprint,还有dumpit和truecrypt volatility -f mem.raw --profi ...
- python第三方库安装与卸载
一.检查python环境是否正常 python安装完毕并设置环境变量后,可在cmd中运行python查看,显示版本等信息 二.查看已经安装的第三方库 通过pip list可查看已安装的库,以及对应的 ...
- 加不加 synchronized 有什么区别?
今天一起来认识认识 synchronized 这个一面试就会被提到的关键字.这一篇不会讲太多理论,主要先熟悉熟悉一下最简单的用法.只讨论一个问题:方法没用 synchronized 和用了 synch ...
- python face_recognition模块实现人脸识别
import face_recognition #人脸识别库 pip cmake dlib import cv2 #读取图像 face_image1 = face_recognition.load_i ...
- centos7下端口映射
firewall-cmd --zone=external --add-forward-port=port=:proto=tcp:toport=:toaddr=192.168.10.10 --perma ...
- Spring5参考指南:IOC容器
文章目录 为什么使用Spring5 什么是IOC容器 配置元数据 实例化容器 XML嵌套 groovy bean定义DSL 使用容器 最近在翻译Spring Framework Documentati ...