当前位置:网站首页>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
边栏推荐
- 16 医疗挂号系统_【预约下单】
- Several silly built-in functions about relative path / absolute path operation in CAPL script
- MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
- 实现以form-data参数发送post请求
- max-flow min-cut
- 17 医疗挂号系统_【微信支付】
- [flask] crud addition and query operation of data
- MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
- cmooc互联网+教育
- 软件测试工程师必备之软技能:结构化思维
猜你喜欢

C miscellaneous lecture continued

A necessary soft skill for Software Test Engineers: structured thinking
![[CV] target detection: derivation of common terms and map evaluation indicators](/img/e8/04cc8336223c0ab2dea5638def88df.jpg)
[CV] target detection: derivation of common terms and map evaluation indicators

CANoe仿真功能之自动化序列(Automation Sequences )

四川云教和双师模式

How to make shell script executable

如何让shell脚本变成可执行文件

CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数

Which is the better prospect for mechanical engineer or Electrical Engineer?
![[NLP] bert4vec: a sentence vector generation tool based on pre training](/img/fd/8e5e1577b4a6ccc06e29350a1113ed.jpg)
[NLP] bert4vec: a sentence vector generation tool based on pre training
随机推荐
CANoe CAPL文件操作目录合集
History of object recognition
Some thoughts on the study of 51 single chip microcomputer
Teach you how to write the first MCU program hand in hand
软件测试工程师发展规划路线
Can I learn PLC at the age of 33
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
How to build an interface automation testing framework?
Cmooc Internet + education
How does the single chip microcomputer execute the main function from power on reset?
竞赛vscode配置指南
MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
Canoe CAPL file operation directory collection
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
Several errors encountered when installing opencv
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?
MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
软件测试工程师必备之软技能:结构化思维
MySQL实战优化高手10 生产经验:如何为数据库的监控系统部署可视化报表系统?
在CANoe中通过Panel面板控制Test Module 运行(初级)