Example #1
0
  public void execute() {

    client = new HttpClient();
    //		if (requestType.equals("GET")) {
    // Instantiate a GET HTTP method
    //	        HttpMethod method = new GetMethod(apiUrl);

    //	        if (!authentication.equals("")) {
    //	        	method.setRequestHeader("Authorization", "basic " + authentication);
    //	        }
    try {

      int statusCode = client.executeMethod(method);

      System.out.println("QueryString>>> " + apiUrl);
      System.out.println("Status Text>>>" + HttpStatus.getStatusText(statusCode));

      // Get data as a String
      System.out.println(method.getResponseBodyAsString());

      // OR as a byte array
      byte[] res = method.getResponseBody();

      // write to file
      FileOutputStream fos = new FileOutputStream("donepage.html");
      fos.write(res);

      // release connection
      method.releaseConnection();
    } catch (IOException e) {
      e.printStackTrace();
    }
    // }
  }
Example #2
0
  protected void construct(String legalurl) throws HTTPException {
    this.legalurl = legalurl;
    try {
      sessionClient = new HttpClient(connmgr);
      HttpClientParams clientparams = sessionClient.getParams();

      // Allow (circular) redirects
      clientparams.setParameter(ALLOW_CIRCULAR_REDIRECTS, true);
      clientparams.setParameter(MAX_REDIRECTS, 25);

      if (globalSoTimeout > 0) setSoTimeout(globalSoTimeout);

      if (globalConnectionTimeout > 0) setConnectionTimeout(globalConnectionTimeout);

      if (globalAgent != null) setUserAgent(globalAgent); // May get overridden by setUserAgent

      setAuthenticationPreemptive(globalauthpreemptive);

      setProxy();

      if (TESTING) HTTPSession.track(this);

    } catch (Exception e) {
      throw new HTTPException("url=" + legalurl, e);
    }
  }
Example #3
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;
 }
Example #4
0
 // All proxy activity goes thru here
 void setProxy(Proxy proxy) {
   if (sessionClient == null) return;
   if (proxy != null && proxy.host != null)
     sessionClient.getHostConfiguration().setProxy(proxy.host, proxy.port);
 }
Example #5
0
 public void clearState() {
   sessionClient.getState().clearCookies();
   sessionClient.getState().clearCredentials();
 }
Example #6
0
 public void setMaxRedirects(int n) {
   HttpClientParams clientparams = sessionClient.getParams();
   clientparams.setParameter(MAX_REDIRECTS, n);
 }
Example #7
0
 public Cookie[] getCookies() {
   if (sessionClient == null) return null;
   Cookie[] cookies = sessionClient.getState().getCookies();
   return cookies;
 }
Example #8
0
 public String getCookiePolicy() {
   return sessionClient == null ? null : sessionClient.getParams().getCookiePolicy();
 }
Example #9
0
 public void setConnectionTimeout(int timeout) {
   sessionClient.setConnectionTimeout(timeout);
 }
Example #10
0
 public void setSoTimeout(int timeout) {
   sessionClient.getParams().setSoTimeout(timeout);
 }
Example #11
0
 public void setAuthenticationPreemptive(boolean tf) {
   if (sessionClient != null) sessionClient.getParams().setAuthenticationPreemptive(tf);
 }
Example #12
0
 public void setUserAgent(String agent) {
   useragent = agent;
   if (useragent != null && sessionClient != null)
     sessionClient.getParams().setParameter(USER_AGENT, useragent);
 }
Example #13
0
 public static Cookie[] getGlobalCookies() {
   HttpClient client = new HttpClient(connmgr);
   Cookie[] cookies = client.getState().getCookies();
   return cookies;
 }