
使用jar包:
1<!--commons-httpclient--> 2<dependency> 3 <groupId>commons-httpclient</groupId> 4 <artifactId>commons-httpclient</artifactId> 5 <version>3.1</version> 6 </dependency> 7</dependencies>
代码:
1try { 2 String postURL = "http://*.*.*.*:8080/cjs/ad"; 3 PostMethod postMethod = null; 4 postMethod = new PostMethod(postURL) ; //添加请求头数据 5 postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ; 6 //参数设置,需要注意的就是里边不能传NULL,要传空字符串 7 NameValuePair[] data = { 8 new NameValuePair("_ArgStr1","*"), new NameValuePair("_ArgStr2","*") 9 }; 10 11 postMethod.setRequestBody(data); 12 13 org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(); 14 int response = httpClient.executeMethod(postMethod); // 执行POST方法 15 String result = postMethod.getResponseBodyAsString() ; 16 System.out.println(response); 17 System.out.println(result); 18 } catch (Exception e) { 19 // logger.info("请求异常"+e.getMessage(),e); 20 throw new RuntimeException(e.getMessage()); 21 }