httpClient 3

HttpClient httpClient = new HttpClient();cookie

  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);
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息