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类发 ...
随机推荐
- [一起读源码]走进C#并发队列ConcurrentQueue的内部世界 — .NET Core篇
在上一篇<走进C#并发队列ConcurrentQueue的内部世界>中解析了Framework下的ConcurrentQueue实现原理,经过抛砖引玉,得到了一众大佬的指点,找到了.NET ...
- 今天整理了几个在使用python进行数据分析的常用小技巧、命令。
提高Python数据分析速度的八个小技巧 01 使用Pandas Profiling预览数据 这个神器我们在之前的文章中就详细讲过,使用Pandas Profiling可以在进行数据分析之前对数据进行 ...
- C - 剪花布条 (KMP例题)
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和 ...
- 教你如何入手用python实现简单爬虫微信公众号并下载视频
主要功能 如何简单爬虫微信公众号 获取信息:标题.摘要.封面.文章地址 自动批量下载公众号内的视频 一.获取公众号信息:标题.摘要.封面.文章URL 操作步骤: 1.先自己申请一个公众号 2.登录自己 ...
- docker环境常用命令
Ubuntu 安装docker及docker-compose 安装: apt-get install docker apt-get install docker-compose 启动docker环境: ...
- Spring5:IOC注解
使用注解须知: 1:导入约束:导入context的命名空间 2:配置注解的支持:<context:annotation-config/> <?xml version="1. ...
- Linux-Discuz安装LAMP
1.下载,解压Discuz cd /data/discuz wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GBK.zip un ...
- sk-learn实现L2岭回归,对线性回归正则化
岭回归算法: from sklearn.datasets import load_boston from sklearn.externals import joblib from sklearn.li ...
- Java 多线程 -- 线程安全 双重检测(double checking)
先看一个经典的12306案例: public class SynBlockTest { public static void main(String[] args) { // 一份资源 SynWeb1 ...
- 【转帖】Python 重复造轮子/造轮子找模子,你都应该熟读该文
Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...