平时都是喜欢用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参数组装