当前位置:网站首页>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
边栏推荐
- The governor of New Jersey signed seven bills to improve gun safety
- Listen to my advice and learn according to this embedded curriculum content and curriculum system
- Preliminary introduction to C miscellaneous lecture document
- flask运维脚本(长时间运行)
- 竞赛vscode配置指南
- Zsh configuration file
- The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
- MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
- MySQL ERROR 1040: Too many connections
- MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
猜你喜欢
Super detailed steps for pushing wechat official account H5 messages
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
Installation de la pagode et déploiement du projet flask
[flask] crud addition and query operation of data
jar运行报错no main manifest attribute
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位
The programming ranking list came out in February. Is the result as you expected?
[after reading the series of must know] one of how to realize app automation without programming (preparation)
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
随机推荐
15 医疗挂号系统_【预约挂号】
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
Several errors encountered when installing opencv
Sichuan cloud education and double teacher model
flask运维脚本(长时间运行)
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
[flask] crud addition and query operation of data
[after reading the series of must know] one of how to realize app automation without programming (preparation)
C杂讲 文件 续讲
The replay block of canoe still needs to be combined with CAPL script to make it clear
MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
Competition vscode Configuration Guide
Teach you how to write the first MCU program hand in hand
Installation of pagoda and deployment of flask project
VH6501学习系列文章
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
C杂讲 动态链表操作 再讲
CAPL 脚本对.ini 配置文件的高阶操作