当前位置:网站首页>实现以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
边栏推荐
- NLP路线和资源
- 美新泽西州州长签署七项提高枪支安全的法案
- CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
- Installation of pagoda and deployment of flask project
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- Docker MySQL solves time zone problems
- Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
- 美疾控中心:美国李斯特菌疫情暴发与冰激凌产品有关
- MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
猜你喜欢
Southwest University: Hu hang - Analysis on learning behavior and learning effect
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
C miscellaneous shallow copy and deep copy
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
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.
How can I take a shortcut to learn C language in college
History of object recognition
C杂讲 双向循环链表
13 medical registration system_ [wechat login]
随机推荐
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
在CANoe中通过Panel面板控制Test Module 运行(高级)
MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
Preliminary introduction to C miscellaneous lecture document
Elk project monitoring platform deployment + deployment of detailed use (II)
max-flow min-cut
Programmation défensive en langage C dans le développement intégré
Why is 51+ assembly in college SCM class? Why not come directly to STM32
软件测试工程师必备之软技能:结构化思维
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
实现微信公众号H5消息推送的超级详细步骤
51单片机进修的一些感悟
Carolyn Rosé博士的社交互通演讲记录
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
The real future of hardware engineers may not be believed by you if I say so
Vh6501 Learning Series
Some thoughts on the study of 51 single chip microcomputer
Listen to my advice and learn according to this embedded curriculum content and curriculum system
Notes of Dr. Carolyn ROS é's social networking speech