当前位置:网站首页>实现以form-data参数发送post请求
实现以form-data参数发送post请求
2022-07-06 09:10:00 【Eric-x】
在对接一个第三方接口的时候,发现 请求参数Body要以multipart/form-data 方式提交。然后就去收集了一下资料,特此记录一下
/** * 以post方式调用第三方接口,以form-data 形式 发送数据 * * @param url post请求url * @param paramMap 表单里其他参数 * @return */
public static String doPost(String url, Map<String, String> paramMap) {
// 创建Http实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpPost实例
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(java.nio.charset.Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//表单中参数
for(Map.Entry<String, String> entry: paramMap.entrySet()) {
builder.addPart(entry.getKey(),new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
}
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 返回
String res = EntityUtils.toString(response.getEntity(), java.nio.charset.Charset.forName("UTF-8"));
return res;
}
} catch (Exception e) {
e.printStackTrace();
logger.error("调用HttpPost失败!" + e.toString());
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
logger.error("关闭HttpPost连接失败!");
}
}
}
return null;
}
然后放一下我使用的依赖
<!-- httpclient 相关依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
然后如果使用PostMan以这种方式测试的话,是这样的。
参考:https://blog.csdn.net/LYY1448019681/article/details/115124407
边栏推荐
- Some thoughts on the study of 51 single chip microcomputer
- docker MySQL解决时区问题
- Tianmu MVC audit I
- Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
- flask运维脚本(长时间运行)
- 零基础学习单片机切记这四点要求,少走弯路
- Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
- CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
- Which is the better prospect for mechanical engineer or Electrical Engineer?
- MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
猜你喜欢

C杂讲 文件 续讲

解决在window中远程连接Linux下的MySQL

MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?

CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?

Redis集群方案应该怎么做?都有哪些方案?

MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?

C miscellaneous dynamic linked list operation

CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白

Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
![15 medical registration system_ [appointment registration]](/img/c1/27c7a5aae82783535e5467583bb176.png)
15 medical registration system_ [appointment registration]
随机推荐
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
Why is 51+ assembly in college SCM class? Why not come directly to STM32
Mexican SQL manual injection vulnerability test (mongodb database) problem solution
C miscellaneous dynamic linked list operation
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
Why can't TN-C use 2p circuit breaker?
Installation de la pagode et déploiement du projet flask
Sichuan cloud education and double teacher model
安装OpenCV时遇到的几种错误
MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
The replay block of canoe still needs to be combined with CAPL script to make it clear
[CV] target detection: derivation of common terms and map evaluation indicators
Tianmu MVC audit II
Safety notes
Target detection -- yolov2 paper intensive reading
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
docker MySQL解决时区问题
嵌入式开发比单片机要难很多?谈谈单片机和嵌入式开发设计经历