当前位置:网站首页>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
边栏推荐
- Listen to my advice and learn according to this embedded curriculum content and curriculum system
- 13 医疗挂号系统_【 微信登录】
- 软件测试工程师必备之软技能:结构化思维
- Learning SCM is of great help to society
- Vh6501 Learning Series
- C杂讲 文件 初讲
- Super detailed steps for pushing wechat official account H5 messages
- Sichuan cloud education and double teacher model
- 16 medical registration system_ [order by appointment]
- 简单解决phpjm加密问题 免费phpjm解密工具
猜你喜欢
What should the redis cluster solution do? What are the plans?
13 medical registration system_ [wechat login]
软件测试工程师必备之软技能:结构化思维
CAPL script pair High level operation of INI configuration file
CANoe不能自动识别串口号?那就封装个DLL让它必须行
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Implement context manager through with
If a university wants to choose to study automation, what books can it read in advance?
[after reading the series of must know] one of how to realize app automation without programming (preparation)
C miscellaneous two-way circular linked list
随机推荐
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.
MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
竞赛vscode配置指南
How does the single chip microcomputer execute the main function from power on reset?
Retention policy of RMAN backup
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
17 医疗挂号系统_【微信支付】
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
CANoe CAPL文件操作目录合集
Target detection -- yolov2 paper intensive reading
C杂讲 文件 初讲
If someone asks you about the consistency of database cache, send this article directly to him
Flash operation and maintenance script (running for a long time)
寶塔的安裝和flask項目部署
Notes of Dr. Carolyn ROS é's social networking speech
实现以form-data参数发送post请求
The programming ranking list came out in February. Is the result as you expected?
A necessary soft skill for Software Test Engineers: structured thinking
Control the operation of the test module through the panel in canoe (primary)
Write your own CPU Chapter 10 - learning notes