当前位置:网站首页>实现以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 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
- Jar runs with error no main manifest attribute
- Docker MySQL solves time zone problems
- Several silly built-in functions about relative path / absolute path operation in CAPL script
- If a university wants to choose to study automation, what books can it read in advance?
- 再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
- docker MySQL解决时区问题
- C miscellaneous two-way circular linked list
- NLP路线和资源
- Random notes
猜你喜欢

If a university wants to choose to study automation, what books can it read in advance?
![[CV] target detection: derivation of common terms and map evaluation indicators](/img/e8/04cc8336223c0ab2dea5638def88df.jpg)
[CV] target detection: derivation of common terms and map evaluation indicators

CANoe不能自动识别串口号?那就封装个DLL让它必须行

CAPL 脚本对.ini 配置文件的高阶操作

Why can't TN-C use 2p circuit breaker?

Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience

单片机实现模块化编程:思维+实例+系统教程(实用程度令人发指)

Can I learn PLC at the age of 33

颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
随机推荐
在CANoe中通过Panel面板控制Test Module 运行(初级)
15 medical registration system_ [appointment registration]
[flask] crud addition and query operation of data
嵌入式开发中的防御性C语言编程
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
C miscellaneous two-way circular linked list
软件测试工程师必备之软技能:结构化思维
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
A new understanding of RMAN retention policy recovery window
在CANoe中通過Panel面板控制Test Module 運行(初級)
Control the operation of the test module through the panel in canoe (Advanced)
Installation of pagoda and deployment of flask project
The 32-year-old fitness coach turned to a programmer and got an offer of 760000 a year. The experience of this older coder caused heated discussion
Canoe CAPL file operation directory collection
简单解决phpjm加密问题 免费phpjm解密工具
NLP routes and resources
vscode 常用的指令
C杂讲 浅拷贝 与 深拷贝
Learning SCM is of great help to society
四川云教和双师模式