当前位置:网站首页>实现以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
边栏推荐
- MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
- Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- C miscellaneous shallow copy and deep copy
- Upload vulnerability
- Simple solution to phpjm encryption problem free phpjm decryption tool
- 在CANoe中通过Panel面板控制Test Module 运行(高级)
- 寶塔的安裝和flask項目部署
- Redis集群方案应该怎么做?都有哪些方案?
- MySQL ERROR 1040: Too many connections
- Teach you how to write the first MCU program hand in hand
猜你喜欢

宝塔的安装和flask项目部署

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

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

The programming ranking list came out in February. Is the result as you expected?

MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计

四川云教和双师模式

Several silly built-in functions about relative path / absolute path operation in CAPL script

A necessary soft skill for Software Test Engineers: structured thinking
![15 medical registration system_ [appointment registration]](/img/c1/27c7a5aae82783535e5467583bb176.png)
15 medical registration system_ [appointment registration]

17 医疗挂号系统_【微信支付】
随机推荐
C miscellaneous shallow copy and deep copy
Teach you how to write the first MCU program hand in hand
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
Random notes
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Some thoughts on the study of 51 single chip microcomputer
单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
16 medical registration system_ [order by appointment]
CAPL 脚本对.ini 配置文件的高阶操作
Flash operation and maintenance script (running for a long time)
Compress decompress
Pointer learning
好博客好资料记录链接
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
[flask] crud addition and query operation of data
Tianmu MVC audit I
Can I learn PLC at the age of 33
MySQL ERROR 1040: Too many connections