springboot 访问httpclient接口
使用jar:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.4</version>
</dependency>
工具类:
import java.util.HashMap;
import java.util.Map;import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;/*** * @title: httpclient 工具类* @author: wll* @since: 2021-5-27 10:00:27*/
public class HttpClientUtil {private static final int socketTimeout = 10 * 60 * 1000;// 请求超时时间十分钟private static final int connectTimeout = 10 * 60 * 1000;// 传输超时时间十分钟private static final String ENCODING = "UTF-8";// 编码格式。发送编码格式统一用UTF-8/*** POST方式请求参数* * @param url 请求地址* @param heads 请求头* @param jsonParam 请求参数* @return* @throws Exception*/public static String postFun(String url, Map<String, String> heads, String jsonParam) throws Exception {System.out.println("------>HttpClient POST DATA url:" + url + ",heads:" + heads + ",reqStr:" + jsonParam);CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost post = new HttpPost(url);// 设置请求和传输超时时间RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();post.setConfig(requestConfig);// 设置头部参数if (heads != null && !heads.isEmpty()) {for (Map.Entry<String, String> entry : heads.entrySet()) {post.addHeader(entry.getKey(), entry.getValue());}}// 请求body参数if (StringUtils.isNotBlank(jsonParam)) {StringEntity entity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON);post.setEntity(entity);}CloseableHttpResponse resp= httpclient.execute(post);String resultStr = EntityUtils.toString(resp.getEntity(), ENCODING);System.out.println("------>HttpClient POST RESULT DATA:" + resultStr);return resultStr;}/*** HttpClient测试* * @param args* @throws Exception*/public static void main(String[] args) throws Exception {Map<String, String> heads = new HashMap<>(16);heads.put("dataType", "json");heads.put("contentType", "application/json;charset=UTF-8");String postUrl = "http://127.0.0.1:8081/xxxx/xxxx/xxx/xxx.do";String idcardId = "";String paramJson = "{\"idcard\":\"" + idcardId + "\",\"state\":\"0\"}";String resStr = HttpClientUtil.postFun(postUrl, heads, paramJson);System.out.println("----------->" + resStr);}
}
springboot 访问httpclient接口相关推荐
- 前端请求接口post_程序员:HttpClient进行post请求的工具类,访问第三方接口HTTPS...
HTTPS (英语:Hypertext Transfer Protocol Secure,缩写:HTTPS,常称为HTTP over TLS,HTTP over SSL或HTTP Secure) 是一 ...
- SpringBoot使用RestTemplate访问第三方接口
养成习惯,先赞后看!!! 目录 前言 介绍 HTTPClient与RestTemplate 使用 第一步-注入bean 第二步-调用方法 getForEntity与getForObject的区别 ge ...
- spring resttemplate 中文参数_SpringBoot使用RestTemplate访问第三方接口
养成习惯,先赞后看!!! 前言 相信大家都知道如何在自己的项目里面调用自己的接口,只需要调用自己项目里面接口特定的URL地址就行了,但是如果是调用其他项目的接口呢,这时候如果是直接调用的话,很明显我们 ...
- c#分页_使用Kotlin搭配Springboot开发RESTFul接口(二)自定义配置、跨域、分页
前言 上一篇文章请看这里:使用Kotlin搭配Springboot开发RESTFul接口与服务部署 上一篇文章介绍了Kotlin搭配Springboot的开发流程,从搭建项目.ORM.Controll ...
- SpringBoot 如何生成接口文档,老鸟们都这么玩的!
为什么要用Swagger ? " 作为一名程序员,我们最讨厌两件事:1. 别人不写注释.2. 自己写注释. 而作为一名接口开发者,我们同样讨厌两件事:1. 别人不写接口文档,文档不及时更新. ...
- 部署和访问HTTP接口
项目发布Http接口和访问该接口 一.原理:当调用者调用接口的时候是发送url请求,所有类似于我们在浏览器直接输入地址发送请求一样.至于处理请求像现在的mvc框架都可以处理,比如servlet和str ...
- SpringBoot中通过接口下载resources下的文件
SpringBoot中通过接口下载resources下的文件 解决的问题 当项目打成jar包进行部署时,一些示例文件放在resources目录下,提供接口供用户访问下载 文件存放位置 接口实现 @Re ...
- 关于访问后端接口报404的问题——全网最详细的404错误详解
当我们通过前端向后端发起一个请求调用后端接口时,经常会遇到404的问题.网上关于对404问题介绍的一大堆,其实404问题的本质就两点. 在介绍404问题之前先温习一个小的知识点--项目访问路径 项目访 ...
- 如何将访问的接口去掉token验证
如何将访问的接口去掉token验证 项目应用:springboot oath2 完成此操作需要修改两个模块的application.yml文件修改,一个是getway网关的yml文件,另一个则是接口所 ...
最新文章
- 细菌绝对定量的方法总结
- JVM-运行时数据区
- NB!技术人都在关注的公众号~
- 第7章:MapReduce编程
- android+4.4+稳定性,新系统新UI!台电A10h四核安卓4.4.2性能测试
- 哎呀你不要把他叫出来_不吼不叫,学会这3个亲子互动方法,孩子不专注都难...
- Android Paint 画笔使用详解 Android自定义View(六)
- 实现jdbc连接mysql_Java JDBC连接MYSQL数据库教程(实现)
- CSS:CSS定位和浮动
- 怎么成为日上会员直邮_放福利啦,免税店现在一件也能直邮,不用出入境、不用出入境、不用找代购...
- Linux运维之ntpdate同步网络时间
- MasterPage嵌套及MasterPage中的控件和变量的访问
- jsp在线预览Word文档操作步骤(自己测试的)
- JavaScript - 屏蔽鼠标右键菜单
- CTF隐写术:男神一般都很低调很低调的
- 返回值为对象调用拷贝构造函数
- RGB-D SLAM in Dynamic Environments Using Static Point Weighting论文笔记
- 正规的IT外包公司的报价组成
- 木讷的程序员需要知道的事情 (三)
- RuntimeError: mat1 and mat2 shapes cannot be multiplied