当前位置:网站首页>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
边栏推荐
- How to make shell script executable
- Installation de la pagode et déploiement du projet flask
- 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
- Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
- MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
- 颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
- MySQL ERROR 1040: Too many connections
- Good blog good material record link
- CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
- 西南大学:胡航-关于学习行为和学习效果分析
猜你喜欢
Preliminary introduction to C miscellaneous lecture document
Automation sequences of canoe simulation functions
实现微信公众号H5消息推送的超级详细步骤
软件测试工程师必备之软技能:结构化思维
Redis集群方案应该怎么做?都有哪些方案?
软件测试工程师必备之软技能:结构化思维
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
15 医疗挂号系统_【预约挂号】
华南技术栈CNN+Bilstm+Attention
What should the redis cluster solution do? What are the plans?
随机推荐
Which is the better prospect for mechanical engineer or Electrical Engineer?
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
Configure system environment variables through bat script
Installation of pagoda and deployment of flask project
cmooc互联网+教育
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
15 医疗挂号系统_【预约挂号】
[Julia] exit notes - Serial
16 medical registration system_ [order by appointment]
If a university wants to choose to study automation, what books can it read in advance?
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实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
实现微信公众号H5消息推送的超级详细步骤
MySQL ERROR 1040: Too many connections
Competition vscode Configuration Guide
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
Routes and resources of AI
The real future of hardware engineers may not be believed by you if I say so