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 ...
随机推荐
- [FAQ] Windows 终端 git status 不识别文件名大小写的修改
当我们修改了文件名的大小写,git status 显示没有文件改动. 出现这种情况,首先看一下 git 的配置项是否忽略了文件问大小写: $ git config core.ignorecase ...
- Git 工具下载慢问题 & 图像化界面工具
Git 命令行淘宝镜像:git-for-windows Mirror (taobao.org) Git 图形客户端:Download – TortoiseGit – Windows Shell Int ...
- ruby后台传变量到前端的JS里
后端变量 @namelist 前端解释 var result_json = <%= @namelist.to_json.html_safe %>
- JWT 构建Rails API 授权登录
目录 安装jwt组件 创建base_controller.rb控制器 配置路由 用户登陆-api接口 验证用户token信息-api接口(*) 参考下面的连接 使用Rails构建JSON-API ht ...
- html布局浅谈
现在布局方式主要分为三种 传统css布局方案(position,float,line-height等配合).实现复杂,需要多种属性配合使用,兼容性最好. flex布局方案.弹性布局,实现方便,兼容性较 ...
- Selenium4自动化测试8--控件获取数据--上传、下载、https和切换分页
10-上传 上传不能模拟用户在页面上选择本地文件,只能先把要上传的文件先准备好在代码里上传 import time from selenium.webdriver.support.select imp ...
- mybatis-plus id在高并发下出现重复
mybaits-plus ASSIGN_ID生成 id生成策略 在分布式高并发环境下出现重复id https://github.com/baomidou/mybatis-plus/issues/307 ...
- 深度学习项目-MobileNetV2水果识别模型
FruitRecognition DeepLearning深度学习小项目,利用CNN和MobileNetV2搭建的水果识别模型. github地址 fruit为本次大作业使用的数据集. geneFru ...
- 怀念中的java
学了这门语言后一直没能做成项目,倒是安装环境,用记事本编辑的话,除了js最好做的就是java了. 以前学java的时候是一帮很有朝气的同学,在一个培训班,每天苦哈哈.从c开始学的语言,学完基础部分转入 ...
- powershell 设置代理
$env:HTTP_PROXY="http://127.0.0.1:10809" $env:HTTPS_PROXY="http://127.0.0.1:10809&quo ...