httpClient 3

HttpClient httpClient = new HttpClient();

  HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
  // 设置连接超时时间(单位毫秒)
  managerParams.setConnectionTimeout(30000);
  // 设置读数据超时时间(单位毫秒)
  managerParams.setSoTimeout(120000);
  String url = "localhost";
  PostMethod postMethod = new PostMethod(url);
  Header  header = new Header();
  header.setName("Cookie");
  header.setValue("cookie");
  header.setName("Refer");
  header.setValue("refer");
  postMethod.setRequestHeader(header);
  String strResponse = null;
  int statusCode=-1;
  try
  {
    statusCode= httpClient.executeMethod(postMethod);
    if (statusCode != HttpStatus.SC_OK)
    {
      throw new IllegalStateException("Method failed: "+ postMethod.getStatusLine());
    }
    strResponse = postMethod.getResponseBodyAsString();
  } catch (Exception ex)
  {
    throw new IllegalStateException(ex.toString());
  }
    finally
  {
     //释放连接
     postMethod.releaseConnection();
  }
    System.out.println(strResponse);
 
posted @ 2015-07-02 11:54  hotarubi  阅读(280)  评论(0编辑  收藏  举报