免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2119 | 回复: 2

大家代码里都怎么发起http请求的~求深入 [复制链接]

论坛徽章:
0
发表于 2012-09-11 23:46 |显示全部楼层
写了一个最简单的demo,用来发起http请求调用。
大家代码里都怎么个搞法?
求深入,求扩展~~
  1.         public static void main(String[] args) throws HttpException, IOException {
  2.                 testHttp(0);
  3.                 testHttp(1);
  4.         }
  5.        
  6.         /**
  7.          * type=0, get
  8.          * type=1, post
  9.          * http://haoma.imobile.com.cn/index.php?mob=18858111994
  10.          */
  11.         public static void testHttp(int type) throws HttpException, IOException{
  12.                 HttpClient client = new HttpClient();
  13.                 client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");
  14.                 HttpMethod method = null;
  15.                 switch(type){
  16.                 case 0:
  17.                         method = getGetMethod();
  18.                         break;
  19.                 case 1:
  20.                         method = getPostMethod();
  21.                         break;
  22.                 }
  23.                
  24.                 client.executeMethod(method);
  25.                 System.out.println("statusLine = " + method.getStatusLine());

  26.                 String response = new String(method.getResponseBodyAsString());
  27.                 System.out.println("response = " + response);
  28.                
  29.                 method.releaseConnection();
  30.         }

  31.         private static HttpMethod getGetMethod() {
  32.                 return new GetMethod("/index.php?mob=18858111994");
  33.         }

  34.         private static HttpMethod getPostMethod() {
  35.                 PostMethod post = new PostMethod("/index.php");
  36.                 NameValuePair simcard = new NameValuePair("mob", "18858111994");
  37.                 post.setRequestBody(new NameValuePair[] { simcard });
  38.                 return post;
  39.         }
复制代码

论坛徽章:
0
发表于 2012-09-16 15:23 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
125
处女座
日期:2014-06-14 02:20:38双子座
日期:2014-06-14 03:59:12处女座
日期:2014-06-14 04:14:31狮子座
日期:2014-06-14 05:24:38巳蛇
日期:2014-06-14 05:48:58巨蟹座
日期:2014-06-14 05:50:18摩羯座
日期:2014-06-14 06:23:58双鱼座
日期:2014-06-14 06:49:15亥猪
日期:2014-06-14 07:04:56巨蟹座
日期:2014-06-14 07:12:32双子座
日期:2014-06-14 07:26:53射手座
日期:2014-06-14 07:58:51
发表于 2012-09-25 10:48 |显示全部楼层
swt                 
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP