当前位置:网站首页>实现以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
边栏推荐
- Zsh configuration file
- CAPL script pair High level operation of INI configuration file
- [untitled]
- 软件测试工程师发展规划路线
- C杂讲 浅拷贝 与 深拷贝
- 16 medical registration system_ [order by appointment]
- Automation sequences of canoe simulation functions
- Constants and pointers
- jar运行报错no main manifest attribute
- MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
猜你喜欢

Cmooc Internet + education
![14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]](/img/c4/81f00c8b7037b5fb4c5df4d2aa7571.png)
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]

If a university wants to choose to study automation, what books can it read in advance?

max-flow min-cut
![16 medical registration system_ [order by appointment]](/img/7f/d94ac2b3398bf123bc97d44499bb42.png)
16 medical registration system_ [order by appointment]

MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine

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

Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work

Mexican SQL manual injection vulnerability test (mongodb database) problem solution

16 医疗挂号系统_【预约下单】
随机推荐
Keep these four requirements in mind when learning single chip microcomputer with zero foundation and avoid detours
Control the operation of the test module through the panel in canoe (Advanced)
Installation of pagoda and deployment of flask project
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
Retention policy of RMAN backup
通过bat脚本配置系统环境变量
Teach you how to write the first MCU program hand in hand
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
在CANoe中通过Panel面板控制Test Module 运行(高级)
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
16 医疗挂号系统_【预约下单】
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
Southwest University: Hu hang - Analysis on learning behavior and learning effect
A new understanding of RMAN retention policy recovery window
Simple solution to phpjm encryption problem free phpjm decryption tool
Which is the better prospect for mechanical engineer or Electrical Engineer?
在CANoe中通过Panel面板控制Test Module 运行(初级)
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide