public static void main(String[] args) {
   HttpClient client = new HttpClient(); // Create an instance of HttpClient.
   GetMethod method = new GetMethod(url); // Create a method instance.	
   method
       .getParams()
       .setParameter(
           HttpMethodParams.RETRY_HANDLER,
           new DefaultHttpMethodRetryHandler(
               3, false)); // Provide custom retry handler is necessary
   try {
     int statusCode = client.executeMethod(method); // Execute the method.
     if (statusCode != HttpStatus.SC_OK) {
       System.err.println("Method failed: " + method.getStatusLine());
     }
     byte[] responseBody = method.getResponseBody(); // Read the response body.
     System.out.println(
         new String(
             responseBody)); // Deal with the response.// Use caution: ensure correct character
                             // encoding and is not binary data
   } catch (HttpException e) {
     System.err.println("Fatal protocol violation: " + e.getMessage());
     e.printStackTrace();
   } catch (IOException e) {
     System.err.println("Fatal transport error: " + e.getMessage());
     e.printStackTrace();
   } finally {
     method.releaseConnection(); // Release the connection.
   }
 }
Beispiel #2
0
  private String DoCmd(String DURL) throws Exception {
    GetMethod gm = new GetMethod(DURL);
    try {
      httpResponseCode = httpClient.executeMethod(gm);

      if (httpResponseCode == 200) {
        String result = gm.getResponseBodyAsString();
        gm.releaseConnection();
        return result;
      }
    } catch (HttpException e) {
      System.out.println("error: " + e);
      throw (e);
    }
    gm.releaseConnection();
    throw (new Exception("Reponse notok"));
  }
  /** use httpclient to get the data */
  public static ByteBuffer getDataFromURI(String uri) throws IOException {

    GetMethod get = new GetMethod(uri);

    try {
      new HttpClient().executeMethod(get);
      return new ByteBuffer(get.getResponseBodyAsStream());
    } finally {
      get.releaseConnection();
    }
  }
Beispiel #4
0
 public boolean isApacheHttpd() throws Exception {
   GetMethod gm = new GetMethod(URL);
   try {
     httpResponseCode = httpClient.executeMethod(gm);
   } catch (HttpException e) {
     System.out.println("error: " + e);
     throw (e);
   }
   gm.releaseConnection();
   Header head = gm.getResponseHeader("Server");
   if (head != null) return head.toString().contains("Apache/2");
   return false;
 }
 /**
  * Test that {@link GetMethod#setQueryString(java.lang.String)} can include a leading question
  * mark.
  */
 public void testGetMethodQueryString() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   GetMethod method = new GetMethod("/");
   method.setQueryString("?hadQuestionMark=true");
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(response.indexOf("QueryString=\"hadQuestionMark=true\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }
 /**
  * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} works with a
  * parameter name but no value.
  */
 public void testGetMethodParameterWithoutValue() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   GetMethod method = new GetMethod("/");
   method.setQueryString(new NameValuePair[] {new NameValuePair("param-without-value", null)});
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(response.indexOf("QueryString=\"param-without-value=\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }
 /**
  * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} works with a
  * parameter name that occurs more than once.
  */
 public void testGetMethodParameterAppearsTwice() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   GetMethod method = new GetMethod("/");
   method.setQueryString(
       new NameValuePair[] {new NameValuePair("foo", "one"), new NameValuePair("foo", "two")});
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(response.indexOf("QueryString=\"foo=one&foo=two\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }
Beispiel #8
0
 // ���� URL ָ�����ҳ
 public String downloadFile(String url) {
   String filePath = null;
   // 1.���� HttpClinet��������
   HttpClient httpClient = new HttpClient();
   // ���� HTTP���ӳ�ʱ 5s
   httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
   // 2.���� GetMethod��������
   GetMethod getMethod = new GetMethod(url);
   // ���� get����ʱ 5s
   getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
   // �����������Դ���
   getMethod
       .getParams()
       .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
   // 3.ִ��GET����
   try {
     int statusCode = httpClient.executeMethod(getMethod);
     // �жϷ��ʵ�״̬��
     if (statusCode != HttpStatus.SC_OK) {
       System.err.println("Method failed: " + getMethod.getStatusLine());
       filePath = null;
     }
     // 4.���� HTTP ��Ӧ����
     byte[] responseBody = getMethod.getResponseBody(); // ��ȡΪ�ֽ�����
     // ������ҳ url ���ɱ���ʱ���ļ���
     filePath =
         "temp\\" + getFileNameByUrl(url, getMethod.getResponseHeader("Content-Type").getValue());
     File file = new File(filePath);
     if (!file.getParentFile().exists()) {
       file.getParentFile().mkdir();
     }
     saveToLocal(responseBody, filePath);
   } catch (HttpException e) {
     // �����������쳣��������Э�鲻�Ի��߷��ص�����������
     System.out.println("�������http��ַ�Ƿ���ȷ");
     e.printStackTrace();
   } catch (IOException e) {
     // ���������쳣
     e.printStackTrace();
   } finally {
     // �ͷ�����
     getMethod.releaseConnection();
   }
   return filePath;
 }
 public void testGetMethodOverwriteQueryString() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   GetMethod method = new GetMethod("/");
   method.setQueryString("query=string");
   method.setQueryString(
       new NameValuePair[] {
         new NameValuePair("param", "eter"), new NameValuePair("para", "meter")
       });
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertFalse(response.indexOf("QueryString=\"query=string\"") >= 0);
     assertTrue(response.indexOf("QueryString=\"param=eter&para=meter\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }
Beispiel #10
0
 /** 根据模版及参数产生静态页面 */
 public boolean createHtmlPage(String url, String htmlFileName) {
   boolean status = false;
   int statusCode = 0;
   try {
     // 创建一个HttpClient实例充当模拟浏览器
     httpClient = new HttpClient();
     // 设置httpclient读取内容时使用的字符集
     httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "gbk");
     // 创建GET方法的实例
     getMethod = new GetMethod(url);
     // 使用系统提供的默认的恢复策略,在发生异常时候将自动重试3次
     getMethod
         .getParams()
         .setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
     // 设置Get方法提交参数时使用的字符集,以支持中文参数的正常传递
     getMethod.addRequestHeader("Content-Type", "text/html;charset=gbk");
     // 执行Get方法并取得返回状态码,200表示正常,其它代码为异常
     statusCode = httpClient.executeMethod(getMethod);
     if (statusCode != 200) {
       logger.fatal("静态页面引擎在解析" + url + "产生静态页面" + htmlFileName + "时出错!");
     } else {
       // 读取解析结果
       sb = new StringBuffer();
       in = getMethod.getResponseBodyAsStream();
       br = new BufferedReader(new InputStreamReader(in));
       while ((line = br.readLine()) != null) {
         sb.append(line + "\n");
       }
       if (br != null) br.close();
       page = sb.toString();
       // 将页面中的相对路径替换成绝对路径,以确保页面资源正常访问
       page = formatPage(page);
       // 将解析结果写入指定的静态HTML文件中,实现静态HTML生成
       writeHtml(htmlFileName, page);
       status = true;
     }
   } catch (Exception ex) {
     logger.fatal("静态页面引擎在解析" + url + "产生静态页面" + htmlFileName + "时出错:" + ex.getMessage());
   } finally {
     // 释放http连接
     getMethod.releaseConnection();
   }
   return status;
 }
Beispiel #11
0
 /**
  * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} works with multiple
  * parameters.
  */
 public void testGetMethodMultiParameters() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   GetMethod method = new GetMethod("/");
   method.setQueryString(
       new NameValuePair[] {
         new NameValuePair("param-one", "param-value"),
         new NameValuePair("param-two", "param-value2"),
         new NameValuePair("special-chars", ":/?~.")
       });
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(
         response.indexOf(
                 "QueryString=\"param-one=param-value&param-two=param-value2&special-chars=:/?~.\"")
             >= 0);
   } finally {
     method.releaseConnection();
   }
 }