Chinaunix

标题: 大家代码里都怎么发起http请求的~求深入 [打印本页]

作者: ladyABC    时间: 2012-09-11 23:46
标题: 大家代码里都怎么发起http请求的~求深入
写了一个最简单的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.         }
复制代码

作者: lloydm    时间: 2012-09-16 15:23
提示: 作者被禁止或删除 内容自动屏蔽
作者: device_memory    时间: 2012-09-25 10:48
swt                 




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2