HttpClient4基础1--通过匿名代理访问网页

来源:互联网 发布:c语言变量 编辑:程序博客网 时间:2024/05/20 15:12

http://llying.iteye.com/blog/456455

HttpClient发布4.0了 而且底层完全重写了,据说无论是效率还是结构都有质的飞跃。 
现在也要与时具进,研究研究。 

Java代码  收藏代码
  1. package test.httpclient4.proxy;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. import org.apache.http.HttpEntity;  
  8. import org.apache.http.HttpHost;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.HttpStatus;  
  11. import org.apache.http.StatusLine;  
  12. import org.apache.http.client.ClientProtocolException;  
  13. import org.apache.http.client.HttpClient;  
  14. import org.apache.http.client.methods.HttpGet;  
  15. import org.apache.http.conn.params.ConnRoutePNames;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.message.BasicStatusLine;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20.   
  21. public class GetHttpByProxy {  
  22.   
  23.     public static void main(String[] args) throws ClientProtocolException,  
  24.             IOException {  
  25.         //实例化一个HttpClient  
  26.         HttpClient httpClient = new DefaultHttpClient();  
  27.         //设定目标站点  
  28.         HttpHost httpHost = new HttpHost("www.shanhe114.com");  
  29.         //设置代理对象 ip/代理名称,端口  
  30.         HttpHost proxy = new HttpHost("192.168.1.28"5608);  
  31.         //对HttpClient对象设置代理  
  32.         httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,  
  33.                 proxy);  
  34.         HttpGet httpGet = new HttpGet("/");  
  35.         //这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾  
  36.         //HttpGet httpGet = new HttpGet("http://www.shanhe114.com/");  
  37.         //HttpResponse response = httpClient.execute(httpGet);  
  38.           
  39.         HttpResponse response = httpClient.execute(httpHost, httpGet);  
  40.         if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){  
  41.             //请求成功  
  42.             //取得请求内容  
  43.             HttpEntity entity = response.getEntity();  
  44.             //显示内容  
  45.             if (entity != null) {  
  46.                 // 显示结果  
  47.                 BufferedReader reader = new BufferedReader(new InputStreamReader(entity  
  48.                         .getContent(), "UTF-8"));  
  49.                 String line = null;  
  50.                 StringBuffer strBuf = new StringBuffer((int) entity.getContentLength());  
  51.                 while ((line = reader.readLine()) != null) {  
  52.                     strBuf.append(line);  
  53.                 }  
  54.                 strBuf.trimToSize();  
  55.                 System.out.println(strBuf.toString());  
  56.             }  
  57.             if (entity != null) {  
  58.                 entity.consumeContent();  
  59.             }  
  60.         }  
  61.     }  
  62. }  

对于显示将结果转换成String以备后续使用,HttpClient已经为我们提供了一个简便方法 
如下 
Java代码  收藏代码
  1. System.out.println(EntityUtils.toString(entity,"utf-8"));  

0 0
原创粉丝点击