HttpClient使用GET方式通過代理服務器讀取頁面的例子

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * HttpClient使用GET方式通過代理服務器讀取頁面的例子。
 *
 * @author JAVA世紀網(java2000.net, laozizhu.com)
 */
public class HttpClientGet {
  public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    // 訪問的目標站點,端口和協議
    HttpHost targetHost = new HttpHost("www.java2000.net");
    // 代理的設置
    HttpHost proxy = new HttpHost("10.60.8.20", 8080);
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    // 目標地址
    HttpGet httpget = new HttpGet("/");
    System.out.println("目標: " + targetHost);
    System.out.println("請求: " + httpget.getRequestLine());
    // 執行
    HttpResponse response = httpclient.execute(targetHost, httpget);
    HttpEntity entity = response.getEntity();
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
      System.out.println("Response content length: " + entity.getContentLength());
    }
    // 顯示結果
    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
    String line = null;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    if (entity != null) {
      entity.consumeContent();
    }
  }
}

 

2、HttpClient 4 使用POST方式提交普通表單數據的例子

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * HttpClient 4 使用POST方式提交普通表單數據的例子.
 *
 * @author JAVA世紀網(java2000.net, laozizhu.com)
 */
public class HttpClientPost {
  public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    // 代理的設置
    HttpHost proxy = new HttpHost("10.60.8.20", 8080);
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    // 目標地址
    HttpPost httppost = new HttpPost("http://www.java2000.net/login.jsp");
    System.out.println("請求: " + httppost.getRequestLine());
    // 構造最簡單的字符串數據
    StringEntity reqEntity = new StringEntity("username=test&password=test");
    // 設置類型
    reqEntity.setContentType("application/x-www-form-urlencoded");
    // 設置請求的數據
    httppost.setEntity(reqEntity);
    // 執行
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
      System.out.println("Response content length: " + entity.getContentLength());
    }
    // 顯示結果
    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
    String line = null;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    if (entity != null) {
      entity.consumeContent();
    }
  }
}

 

3、HttpClient 4.0通過代理訪問Https的代碼例子

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * HttpClient 4.0通過代理訪問Https的代碼例子。
 *
 * @author JAVA世紀網(java2000.net, laozizhu.com)
 */
public class HttpsProxyGet {
  public static void main(String[] args) throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    // 認證的數據
    // 我這裏是瞎寫的,請根據實際情況填寫
    httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20", 8080),
        new UsernamePasswordCredentials("username", "password"));
    // 訪問的目標站點,端口和協議
    HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
    // 代理的設置
    HttpHost proxy = new HttpHost("10.60.8.20", 8080);
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    // 目標地址
    HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");
    System.out.println("目標: " + targetHost);
    System.out.println("請求: " + httpget.getRequestLine());
    System.out.println("代理: " + proxy);
    // 執行
    HttpResponse response = httpclient.execute(targetHost, httpget);
    HttpEntity entity = response.getEntity();
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
      System.out.println("Response content length: " + entity.getContentLength());
    }
    // 顯示結果
    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
    String line = null;
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
    if (entity != null) {
      entity.consumeContent();
    }
  }
}

 

4、HttpClient讀取頁面的使用例子

package com.laozizhu.apache.httpclient;

import java.net.Socket;

import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpRequest;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;

/**
 * HttpClient讀取頁面的使用例子
 * @author 老紫竹(java2000.net)
 *
 */
public class HttpGet {
  public static void main(String[] args) throws Exception {

    HttpParams params = new BasicHttpParams();
    // HTTP 協議的版本,1.1/1.0/0.9
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    // 字符集
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    // 僞裝的瀏覽器類型
    // IE7 是
    // Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
    //
    // Firefox3.03
    // Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
    //
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);

    BasicHttpProcessor httpproc = new BasicHttpProcessor();

    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());

    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("www.java2000.net", 80);

    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {

      String[] targets = { "/", "/help.jsp" };

      for (int i = 0; i < targets.length; i++) {
        if (!conn.isOpen()) {
          Socket socket = new Socket(host.getHostName(), host.getPort());
          conn.bind(socket, params);
        }
        BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
        System.out.println(">> Request URI: " + request.getRequestLine().getUri());

        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
        request.setParams(params);
        httpexecutor.preProcess(request, httpproc, context);
        HttpResponse response = httpexecutor.execute(request, conn, context);
        response.setParams(params);
        httpexecutor.postProcess(response, httpproc, context);

        // 返回碼
        System.out.println("<< Response: " + response.getStatusLine());
        // 返回的文件頭信息
        Header[] hs = response.getAllHeaders();
        for (Header h : hs) {
          System.out.println(h.getName() + ":" + h.getValue());
        }
        // 輸出主體信息
        System.out.println(EntityUtils.toString(response.getEntity()));
        System.out.println("==============");
        if (!connStrategy.keepAlive(response, context)) {
          conn.close();
        } else {
          System.out.println("Connection kept alive...");
        }
      }
    } finally {
      conn.close();
    }
  }
}

        

5、httpclient中文亂碼解決

httpclient默認使用ISO-8859-1讀取http響應的內容,如果內容中包含漢字的話就得動用醜陋的new String(str.getBytes("ISO-8859-1"),"GBK");語句了。

解決辦法就是使用以下配置。

private static final String CONTENT_CHARSET = "GBK";// httpclient讀取內容時使用的字符集

HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);

 

 

6、HttpClient 4處理文件上傳的例子(MultipartEntity)

 

import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * HttpClient 4處理文件上傳的例子(MultipartEntity).<br>
 * 需要 James Mime4j 0.5的版本,0.6的不行。
 *
 * @author JAVA世紀網(java2000.net, laozizhu.com)
 */
public class HttpClientMultipartFormPost {
  public static void main(String[] args) throws Exception {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost");
    // 一個本地的文件
    FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));
    // 一個字符串
    StringBody comment = new StringBody("A binary file of some kind");
    // 多部分的實體
    MultipartEntity reqEntity = new MultipartEntity();
    // 增加
    reqEntity.addPart("bin", bin);
    reqEntity.addPart("comment", comment);
    // 設置
    httppost.setEntity(reqEntity);
    System.out.println("執行: " + httppost.getRequestLine());
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println("返回長度: " + resEntity.getContentLength());
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }
  }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章