平时都是喜欢用JSON,这种也是第一次。这两种的区别就是传递参数类型不一样。废话不多说,直接上代码
(1)、引入maven包
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
(2)、代码实现
try {
String postURL
PostMethod postMethod = null;
postMethod = new PostMethod(postURL) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
NameValuePair[] data = {
new NameValuePair("startTime",""),
new NameValuePair("endTime","")
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int response = httpClient.executeMethod(postMethod); // 执行POST方法
String result = postMethod.getResponseBodyAsString() ;
return result;
} catch (Exception e) {
logger.info("请求异常"+e.getMessage(),e);
throw new RuntimeException(e.getMessage());
}
(3)、POSTMAN参数组装

本文详细介绍如何使用Apache Commons HttpClient库执行HTTP POST请求。通过具体代码示例,展示了如何设置请求头、参数,以及处理响应结果,为开发者提供了一个实用的参考案例。
7778

被折叠的 条评论
为什么被折叠?



