当前位置:网站首页>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
边栏推荐
- Constants and pointers
- MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
- Retention policy of RMAN backup
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- Notes of Dr. Carolyn ROS é's social networking speech
- cmooc互联网+教育
- Simple solution to phpjm encryption problem free phpjm decryption tool
- 实现微信公众号H5消息推送的超级详细步骤
- Inject common SQL statement collation
- 美疾控中心:美国李斯特菌疫情暴发与冰激凌产品有关
猜你喜欢

C杂讲 动态链表操作 再讲

C miscellaneous dynamic linked list operation

MySQL的存储引擎

CANoe不能自动识别串口号?那就封装个DLL让它必须行

Sichuan cloud education and double teacher model
![[flask] crud addition and query operation of data](/img/fc/7a12ede31befed5f42b32b94ae5ecf.jpg)
[flask] crud addition and query operation of data

C miscellaneous lecture continued

Redis集群方案应该怎么做?都有哪些方案?

MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?

MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
随机推荐
Good blog good material record link
oracle sys_ Context() function
max-flow min-cut
CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
Cooperative development in embedded -- function pointer
Constants and pointers
17 医疗挂号系统_【微信支付】
C miscellaneous two-way circular linked list
MySQL combat optimization expert 07 production experience: how to conduct 360 degree dead angle pressure test on the database in the production environment?
NLP routes and resources
Sichuan cloud education and double teacher model
vscode 常用的指令
AI的路线和资源
Upload vulnerability
Installation of pagoda and deployment of flask project
Why can't TN-C use 2p circuit breaker?
软件测试工程师发展规划路线
Redis集群方案应该怎么做?都有哪些方案?
竞赛vscode配置指南
美新泽西州州长签署七项提高枪支安全的法案