当前位置:网站首页>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
边栏推荐
- Configure system environment variables through bat script
- Write your own CPU Chapter 10 - learning notes
- jar运行报错no main manifest attribute
- The governor of New Jersey signed seven bills to improve gun safety
- A new understanding of RMAN retention policy recovery window
- 华南技术栈CNN+Bilstm+Attention
- CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
- MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
- 好博客好资料记录链接
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
猜你喜欢

C miscellaneous dynamic linked list operation

Sichuan cloud education and double teacher model

112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx

CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?

Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work

17 医疗挂号系统_【微信支付】

Which is the better prospect for mechanical engineer or Electrical Engineer?

How to make shell script executable

四川云教和双师模式

Carolyn Rosé博士的社交互通演讲记录
随机推荐
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
[NLP] bert4vec: a sentence vector generation tool based on pre training
NLP routes and resources
宝塔的安装和flask项目部署
Preliminary introduction to C miscellaneous lecture document
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
[CV] target detection: derivation of common terms and map evaluation indicators
C杂讲 双向循环链表
NLP路线和资源
四川云教和双师模式
max-flow min-cut
CANoe不能自动识别串口号?那就封装个DLL让它必须行
AI的路线和资源
MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位
Canoe CAPL file operation directory collection
Constants and pointers
15 medical registration system_ [appointment registration]
Upload vulnerability
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
Redis集群方案应该怎么做?都有哪些方案?