码迷,mamicode.com
首页 > Web开发 > 详细

Http发送post(x-www-form-urlencoded)请求

时间:2020-01-09 10:26:43      阅读:978      评论:0      收藏:0      [点我收藏+]

标签:int   pcl   artifact   string   client   font   The   cli   参数设置   

 技术图片

使用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()); }

Http发送post(x-www-form-urlencoded)请求

标签:int   pcl   artifact   string   client   font   The   cli   参数设置   

原文地址:https://www.cnblogs.com/caozx/p/12169774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!