当前位置:网站首页>实现以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
边栏推荐
猜你喜欢
Carolyn Rosé博士的社交互通演讲记录
Control the operation of the test module through the panel in canoe (primary)
Write your own CPU Chapter 10 - learning notes
C杂讲 双向循环链表
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
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
Regular expressions are actually very simple
Preliminary introduction to C miscellaneous lecture document
max-flow min-cut
MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
随机推荐
Several silly built-in functions about relative path / absolute path operation in CAPL script
Why is 51+ assembly in college SCM class? Why not come directly to STM32
Can I learn PLC at the age of 33
竞赛vscode配置指南
零基础学习单片机切记这四点要求,少走弯路
C杂讲 文件 续讲
How can I take a shortcut to learn C language in college
AI的路线和资源
Tianmu MVC audit I
Jar runs with error no main manifest attribute
Competition vscode Configuration Guide
华南技术栈CNN+Bilstm+Attention
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
Cmooc Internet + education
MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
Preliminary introduction to C miscellaneous lecture document
Control the operation of the test module through the panel in canoe (primary)
[CV] target detection: derivation of common terms and map evaluation indicators