http请求方式-CloseableHttpClient
http请求方式-CloseableHttpClient
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.core.mydemo.http.OrderReqVO;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
* httpclient 实现https请求,4.4版本之后,工具类如下
*/
public class CloseableHttpClientUtils { private static Logger logger = LoggerFactory.getLogger(CloseableHttpClientUtils.class);
static CloseableHttpClient httpClient;
static CloseableHttpResponse httpResponse; public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault(); } /**
* 发送https请求
*
* @throws Exception
*/
public static String sendByHttp(Map<String, Object> params, String url) {
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> listNVP = new ArrayList<NameValuePair>();
if (params != null) {
for (String key : params.keySet()) {
listNVP.add(new BasicNameValuePair(key, params.get(key).toString()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(listNVP, "UTF-8");
logger.info("创建请求httpPost-URL={},params={}", url, listNVP);
httpPost.setEntity(entity);
httpClient = CloseableHttpClientUtils.createSSLClientDefault();
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
return jsObject;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
httpResponse.close();
httpClient.close();
logger.info("请求流关闭完成");
} catch (IOException e) {
logger.info("请求流关闭出错");
e.printStackTrace();
}
}
} public static void main(String[] args) throws Exception {
OrderReqVO data = new OrderReqVO();
data.setOrderNum("111123333");
data.setServerType("take"); Map<String, Object> map = JSONObject.parseObject(JSON.toJSONString(data)); String url = "https://域名/接口名称"; System.out.println("返回结果=" + CloseableHttpClientUtils.sendByHttp(map, url));;
} }
http请求方式-CloseableHttpClient的更多相关文章
- 航信电子发票开发(servlet请求方式)
在系统用户交费后,需要打印发票,可以选择普票或者机打票(票据信息在系统中自定义设置的),也可以打印电子发票,这里对接的是航信的电子发票,请求方式非web服务,而是使用servlet通过HTTP请求的方 ...
- 限制action所接受的请求方式或请求参数
原文:http://www.cnblogs.com/liukemng/p/3726897.html 2.限制action所接受的请求方式(get或post): 之前我们在HelloWorldContr ...
- Android——JDK的get请求方式
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- 第二节(RequestMapping请求方式)学习尚硅谷-springmvc视频教程
项目中,创建测试类SpringMVCTest @Controller @RequestMapping("/springmvc1") public class SpringMVCTe ...
- jQuery中ajax的4种常用请求方式
jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...
- js中同步与异步请求方式
异步请求方式: $.ajax({ url : 'your url', data:{name:value}, cache : false, async : true, type : "POST ...
- WEB API 中HTTP的get、post、put,delete 请求方式
一.WEB API 中HTTP 请求方式的四个主要方法 (GET, PUT, POST, DELETE), 按照下列方式映射为 CURD 操作: 1.POST 用于新建资源,服务端在指定的URI 上创 ...
- Ajax中的get和post两种请求方式的异同
Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别. 1. get是把参数数据队列加到提交表单的A ...
- HTTP 请求方式: GET和POST的比较(转)
GET和POST是HTTP的两个常用方法. 什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 ...
- jQuery中的Ajax几种请求方式
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
随机推荐
- 各位 PHPer,Serverless 正当时
简介:PHP 作为一个开发群体的很大的语言其应用范围相当广泛,Serverless 的理念和 PHP 语言的理念都是让开发者最大精力集中在自己的业务价值.那么 PHP 遇见 Serverless 会 ...
- [Docker] 镜像源配置 for Linux
$ vi /etc/docker/daemon.json { "registry-mirrors": [ "https://docker.mirrors.ustc.edu ...
- dotnet 使用 Infer# 自动分析代码缺陷
本文告诉大家如何使用 Infer# 开源库配合 GitHub 的 Action 实现自动分析代码缺陷,如找到可空引用或线程安全等问题 这是一个在 GitHub 上完全开源的仓库,请看 https:// ...
- The instance of entity type 'Model' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked.
The instance of entity type 'Model' cannot be tracked because another instance with the same key val ...
- 基于三菱Q系列cc-Link的立体仓库控制系统
系统说明: 方案选择: 工艺流程: 触摸屏设计: 程序设计:采用SFC进行编程,结构清晰,逻辑明了 本文章为学习记录,水平有限,望各路大佬们轻喷!!! 转载请注明出处!!!
- WordPress对url做重定向处理
在一个网站进行改版滞后,可能会产生大量的错误的urls,我们想让这些urls还是存在的,并跳转到新的url,此时就要做301重定向. 针对wordpress做重定向,一般推荐使用redirection ...
- IPv6 — 协议头
目录 文章目录 目录 前文列表 IPv6 协议头格式 扩展报头 前文列表 <IPv6 - 网际协议第 6 版> <IPv6 - 地址格式与寻址模式> IPv6 协议头格式 IP ...
- 新手【BUUCTF】逆向writeup()
0x00前言 在大三开始入门逆向,已学完小甲鱼解密篇,刚开始看<加密与解密>,现在沉浸在快 乐的刷题学习中..... buuctf_reverse地址 0x01刚接触的逆向题 revers ...
- 知识图谱-生物信息学-医学顶刊论文(Briefings in Bioinformatics-2022):基于异构图GCN和GAT的DTI预测
(2022.4.16)Briefings-DTI-HETA:基于异构图GCN和GAT的DTI预测 目录 (2022.4.16)Briefings-DTI-HETA:基于异构图GCN和GAT的DTI预测 ...
- 图片GPS度分秒转换经纬度
图片位置信息转化经纬度 1 public static String strToLatOrLng(String str) { 2 int i = str.lastIndexOf("°&quo ...