HttpClient

介绍下一篇上好的文章,对HttpClient有很全很详细的见解https://www.ibm.com/developerworks/cn/opensource/os-cn-crawler/

这个是里面的部分内容

   1:  /* 1 生成 HttpClinet 对象并设置参数*/
   2:    HttpClient httpClient=new HttpClient();
   3:    //设置 Http 连接超时为5秒
   4:  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
   5:    
   6:    /*2 生成 GetMethod 对象并设置参数*/
   7:    GetMethod getMethod=new GetMethod(url);     
   8:    //设置 get 请求超时为 5 秒
   9:  getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,5000);
  10:    //设置请求重试处理,用的是默认的重试处理:请求三次
  11:  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  12:            new DefaultHttpMethodRetryHandler());
  13:    
  14:    /*3 执行 HTTP GET 请求*/
  15:    try{ 
  16:        int statusCode = httpClient.executeMethod(getMethod);
  17:        /*4 判断访问的状态码*/
  18:        if (statusCode != HttpStatus.SC_OK) 
  19:        {
  20:  System.err.println("Method failed: "+ getMethod.getStatusLine());
  21:        }
  22:    
  23:        /*5 处理 HTTP 响应内容*/
  24:        //HTTP响应头部信息,这里简单打印
  25:    Header[] headers=getMethod.getResponseHeaders();
  26:        for(Header  h:  headers)
  27:              System.out.println(h.getName()+" "+h.getValue());*/
  28:        //读取 HTTP 响应内容,这里简单打印网页内容
  29:        byte[] responseBody = getMethod.getResponseBody();//读取为字节数组
  30:  System.out.println(new String(responseBody));
  31:        //读取为 InputStream,在网页内容数据量大时候推荐使用
  32:        InputStream response = getMethod.getResponseBodyAsStream();//
  33:  
  34:  }
  35:  catch (HttpException e) 
  36:  {
  37:        // 发生致命的异常,可能是协议不对或者返回的内容有问题
  38:            System.out.println("Please check your provided http address!");
  39:  e.printStackTrace();
  40:       } 
  41:  catch (IOException e)
  42:    {
  43:              // 发生网络异常
  44:          e.printStackTrace();
  45:       } finally {
  46:                   /*6 .释放连接*/
  47:              getMethod.releaseConnection();           
  48:              }

其实我只是想利用它来模 拟输入用户名和口令进行登录然后访问另一个页面

但是关键问题还是怎么得到NameValuePair name = new NameValuePair("name", "ld"); 里的key和value,还是不明白
   1:  /*
   2:   * Created on 2003-12-7 by Liudong
   3:   */
   4:  package http.demo;
   5:  import org.apache.commons.httpclient.*;
   6:  import org.apache.commons.httpclient.cookie.*;
   7:  import org.apache.commons.httpclient.methods.*;
   8:  /**
   9:   * 用来演示登录表单的示例
  10:   * @author Liudong
  11:   */
  12:  public class FormLoginDemo {
  13:      static final String LOGON_SITE = "localhost";
  14:      static final int    LOGON_PORT = 8080;
  15:     
  16:      public static void main(String[] args) throws Exception{
  17:         HttpClient client = new HttpClient();
  18:         client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);
  19:        
  20:         //模拟登录页面login.jsp->main.jsp
  21:         PostMethod post = new PostMethod("/main.jsp");
  22:         NameValuePair name = new NameValuePair("name", "ld");      
  23:         NameValuePair pass = new NameValuePair("password", "ld");      
  24:         post.setRequestBody(new NameValuePair[]{name,pass});
  25:         int status = client.executeMethod(post);
  26:         System.out.println(post.getResponseBodyAsString());
  27:         post.releaseConnection();  
  28:        
  29:         //查看cookie信息
  30:         CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
  31:         Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());
  32:         if (cookies.length == 0) {
  33:             System.out.println("None");   
  34:         } else {
  35:             for (int i = 0; i < cookies.length; i++) {
  36:                System.out.println(cookies[i].toString());   
  37:             }
  38:         }
  39:         //访问所需的页面main2.jsp
  40:         GetMethod get = new GetMethod("/main2.jsp");
  41:         client.executeMethod(get);
  42:         System.out.println(get.getResponseBodyAsString());
  43:         get.releaseConnection();
  44:      }
  45:  }
posted @ 2015-09-28 16:11  戴杭林  阅读(258)  评论(0编辑  收藏  举报