当前位置:网站首页>实现以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
边栏推荐
- 14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
- C miscellaneous lecture continued
- 颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
- 嵌入式開發中的防禦性C語言編程
- oracle sys_ Context() function
- Delayed note learning
- 51单片机进修的一些感悟
- Simple solution to phpjm encryption problem free phpjm decryption tool
- Some thoughts on the study of 51 single chip microcomputer
- Several silly built-in functions about relative path / absolute path operation in CAPL script
猜你喜欢
Cmooc Internet + education
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
C miscellaneous two-way circular linked list
The replay block of canoe still needs to be combined with CAPL script to make it clear
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.
软件测试工程师必备之软技能:结构化思维
51单片机进修的一些感悟
Write your own CPU Chapter 10 - learning notes
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
随机推荐
C miscellaneous two-way circular linked list
C miscellaneous lecture continued
CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
Upload vulnerability
解决在window中远程连接Linux下的MySQL
好博客好资料记录链接
西南大学:胡航-关于学习行为和学习效果分析
Routes and resources of AI
MySQL的存储引擎
13 medical registration system_ [wechat login]
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Pointer learning
竞赛vscode配置指南
Southwest University: Hu hang - Analysis on learning behavior and learning effect
实现微信公众号H5消息推送的超级详细步骤
Cooperative development in embedded -- function pointer
简单解决phpjm加密问题 免费phpjm解密工具
C miscellaneous shallow copy and deep copy
51单片机进修的一些感悟
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?