Http发送post(x-www-form-urlencoded)请求
使用jar包:
<!--commons-httpclient--> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> </dependencies>
代码:
try { String postURL = "http://*.*.*.*:8080/cjs/ad"; PostMethod postMethod = null; postMethod = new PostMethod(postURL) ; //添加请求头数据 postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ; //参数设置,需要注意的就是里边不能传NULL,要传空字符串 NameValuePair[] data = { new NameValuePair("_ArgStr1","*"),new NameValuePair("_ArgStr2","*") }; 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() ; System.out.println(response); System.out.println(result); } catch (Exception e) { // logger.info("请求异常"+e.getMessage(),e); throw new RuntimeException(e.getMessage()); }
相关推荐
knightwatch 2020-07-19
标题无所谓 2020-03-23
似水流年梦 2020-03-04
Guanjs0 2020-11-09
wmsjlihuan 2020-09-15
shishengsoft 2020-09-15
poplpsure 2020-08-17
CyborgLin 2020-08-15
Richardxx 2020-07-26
sunnyhappy0 2020-07-26
wcqwcq 2020-07-04
chichichi0 2020-06-16
YAruli 2020-06-13
JF0 2020-06-13
84423067 2020-06-12
心丨悦 2020-06-11
zkwgpp 2020-06-04
stoneechogx 2020-06-04