当前位置:网站首页>Implement sending post request with form data parameter
Implement sending post request with form data parameter
2022-07-06 10:14:00 【Eric-x】
When docking with a third-party interface , Find out Request parameters Body To use multipart/form-data Mode submission . Then I went to collect some information , It is hereby recorded that
/** * With post Call the third-party interface , With form-data form send data * * @param url post request url * @param paramMap Other parameters in the form * @return */
public static String doPost(String url, Map<String, String> paramMap) {
// establish Http example
CloseableHttpClient httpClient = HttpClients.createDefault();
// establish HttpPost example
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(java.nio.charset.Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// Parameters in the form
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);// Execute commit
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// return
String res = EntityUtils.toString(response.getEntity(), java.nio.charset.Charset.forName("UTF-8"));
return res;
}
} catch (Exception e) {
e.printStackTrace();
logger.error(" call HttpPost Failure !" + e.toString());
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
logger.error(" close HttpPost The connection fails !");
}
}
}
return null;
}
Then let's put the dependency I use
<!-- httpclient Related dependencies -->
<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>
And if you use PostMan If tested in this way , That's true .
Reference resources :https://blog.csdn.net/LYY1448019681/article/details/115124407
边栏推荐
- Zsh configuration file
- Solve the problem of remote connection to MySQL under Linux in Windows
- C杂讲 浅拷贝 与 深拷贝
- 112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
- 17 medical registration system_ [wechat Payment]
- If a university wants to choose to study automation, what books can it read in advance?
- MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
- Sed text processing
- Tianmu MVC audit II
- 15 医疗挂号系统_【预约挂号】
猜你喜欢
C miscellaneous lecture continued
Hugo blog graphical writing tool -- QT practice
软件测试工程师必备之软技能:结构化思维
Why can't TN-C use 2p circuit breaker?
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
max-flow min-cut
如何让shell脚本变成可执行文件
Solve the problem of remote connection to MySQL under Linux in Windows
C miscellaneous shallow copy and deep copy
在CANoe中通過Panel面板控制Test Module 運行(初級)
随机推荐
Notes of Dr. Carolyn ROS é's social networking speech
Cooperative development in embedded -- function pointer
Target detection -- yolov2 paper intensive reading
Several silly built-in functions about relative path / absolute path operation in CAPL script
CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
[one click] it only takes 30s to build a blog with one click - QT graphical tool
Routes and resources of AI
The governor of New Jersey signed seven bills to improve gun safety
16 医疗挂号系统_【预约下单】
Docker MySQL solves time zone problems
17 medical registration system_ [wechat Payment]
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
Installation de la pagode et déploiement du projet flask
MySQL ERROR 1040: Too many connections
MySQL combat optimization expert 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
MySQL实战优化高手04 借着更新语句在InnoDB存储引擎中的执行流程,聊聊binlog是什么?
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx