当前位置:网站首页>实现以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
边栏推荐
- 如何让shell脚本变成可执行文件
- 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
- Which is the better prospect for mechanical engineer or Electrical Engineer?
- CAPL 脚本对.ini 配置文件的高阶操作
- Function description of shell command parser
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- CANoe仿真功能之自动化序列(Automation Sequences )
- C杂讲 浅拷贝 与 深拷贝
- Zsh configuration file
- max-flow min-cut
猜你喜欢
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
Teach you how to write the first MCU program hand in hand
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
51单片机进修的一些感悟
Why can't TN-C use 2p circuit breaker?
解决在window中远程连接Linux下的MySQL
[flask] crud addition and query operation of data
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
C杂讲 浅拷贝 与 深拷贝
15 medical registration system_ [appointment registration]
随机推荐
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
South China Technology stack cnn+bilstm+attention
如何搭建接口自动化测试框架?
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
Routes and resources of AI
Preliminary introduction to C miscellaneous lecture document
The replay block of canoe still needs to be combined with CAPL script to make it clear
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
51单片机进修的一些感悟
[flask] crud addition and query operation of data
通过bat脚本配置系统环境变量
Vscode common instructions
C miscellaneous lecture continued
Defensive C language programming in embedded development
Tianmu MVC audit II
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?
Random notes
Installation de la pagode et déploiement du projet flask
CANoe CAPL文件操作目录合集