HttpClient

@Override
 public String wakeClusterMachineResolveLog(String url) {
  // TODO Auto-generated method stub
  String responseMsg = "no";
  // System.err.println(" LogServiceImpl wakeClusterMachineResolveLog --->url:"+ url);
  //创建GetMethod的实例
  // 1.构造HttpClient的实例
  HttpClient httpClient = new HttpClient();
  //  2.创建GetMethod的实例  resolveLastLogByCall
  GetMethod getMethod = new GetMethod(url);
  // 使用系统系统的默认的恢复策略   
  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());

  try {
   //3.执行getMethod,调用http接口
   httpClient.executeMethod(getMethod);
   //4.读取内容
   byte[] responseBody = getMethod.getResponseBody();
   responseMsg = new String(responseBody);

  } catch (HttpException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   getMethod.releaseConnection();
  }
  return responseMsg;
 }

注:

HtmlUnit  也可以用于模拟http操作。

相关推荐