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类发 ...
随机推荐
- 数据结构和算法(Golang实现)(5)简单入门Golang-接口
接口 在Golang世界中,有一种叫interface的东西,很是神奇. 一.数据类型 interface{} 如果你事前并不知道变量是哪种数据类型,不知道它是整数还是字符串,但是你还是想要使用它. ...
- AJ学IOS 之ipad开发Popover的基本使用
AJ分享,必须精品 一:效果图 二:注意 对于方法[UIPopoverController dealloc] reached while popover is still visible. 当popo ...
- 试验使用t检验
官方解释 Excel中使用T.TEST函数 T.TEST(array1,array2,tails,type) Array1 必需.第一个数据集. Array2 必需.第二个数据集. ...
- git以及gitHub的使用说明书
一.使用说明 1.Git与github的功能: Git是世界上最先进的分布式版本控制系统,也就是用来记录你的项目代码历史变更信息的工具:github就是用来存储你的代码以及变更信息的云端平台: 2.优 ...
- Java数组模拟环形队列
2.环形队列 (上一篇队列:https://www.cnblogs.com/yxm2020/p/12676323.html) 百度百科 1.假溢出 系统作为队列用的存储区还没有满,但队列却发生了溢 ...
- .NET中 kafka消息队列、环境搭建与使用
前面几篇文章中讲了一些关于消息队列的知识,就每中消息队列中间件,我们并没有做详细的讲解,那么,今天我们就来详细的讲解一下消息队列之一kafka的一些基本的使用与操作. 一.kafka介绍 kafka: ...
- 20200107——记spring的DataSource
spring项目中总要跟数据库打交道,其中怎么连接数据库的方法都有很多,大概分为3类: 1) 通过JNDI获取应用服务器(如JBOSS, Tomcat) 的数据源 2) Spring容器中直接配置数 ...
- SMTP发邮件(直接可用)实例
string file = "邮件测试.txt";//放在Debug下的一个txt文件. MailAddress from = new MailAddress("自己的邮 ...
- Java中常量的概念
常量:在程序执行过程中,其值不发生改变的量.分类:A:字面值常量B:自定义常量字面值常量A:字符串常量(用“”括起来的内容).举例:"hello"B:整数常量 (所有的整数)举例: ...
- kafka相关术语名词
Topic:标签名,一个消息队列的名称 Producer:生产者,发布消息 Consumer:消费者,订阅发布消息,进行处理的存在 Broker:kafka集群,有一个.多个Topic Partiti ...