【转】HttpClient4基础2--通过认证代理访问网页

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

[java]  view plain copy
  1. package test.httpclient4.proxy;     
  2. import java.io.BufferedReader;       
  3. import java.io.IOException;     
  4. import java.io.InputStreamReader;       
  5. import org.apache.http.HttpEntity;       
  6. import org.apache.http.HttpHost;       
  7. import org.apache.http.HttpResponse;       
  8. import org.apache.http.HttpStatus;     
  9. import org.apache.http.auth.AuthScope;     
  10. import org.apache.http.auth.UsernamePasswordCredentials;     
  11. import org.apache.http.client.ClientProtocolException;     
  12. import org.apache.http.client.CredentialsProvider;     
  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.BasicCredentialsProvider;     
  17. import org.apache.http.impl.client.DefaultHttpClient;      
  18. import org.apache.http.util.EntityUtils;     
  19. public class GetHttpByProxyCredentials {     
  20.     
  21.     /**   
  22.      * @param args   
  23.      * @throws IOException    
  24.      * @throws ClientProtocolException    
  25.      */    
  26.     public static void main(String[] args) throws ClientProtocolException, IOException {     
  27.         //实例化一个HttpClient     
  28.         HttpClient httpClient = new DefaultHttpClient();       
  29.         //设定目标站点     
  30.         HttpHost httpHost = new HttpHost("www.shanhe114.com");       
  31.         //设置代理对象 ip/代理名称,端口     
  32.         HttpHost proxy = new HttpHost("proxy.tt"8080);     
  33.         //实例化验证     
  34.         CredentialsProvider credsProvider = new BasicCredentialsProvider();     
  35.         //设定验证内容     
  36.         UsernamePasswordCredentials creds = new UsernamePasswordCredentials("fttj""ft07");     
  37.         //创建验证     
  38.         credsProvider.setCredentials(     
  39.             new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),      
  40.             creds);     
  41.         httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);     
  42.         ((DefaultHttpClient)httpClient).setCredentialsProvider(credsProvider);     
  43.                 
  44.         // 目标地址       
  45.         HttpGet httpget = new HttpGet("/");       
  46.         // 执行       
  47.         HttpResponse response = httpClient.execute(httpHost, httpget);       
  48.         if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){     
  49.             //请求成功     
  50.             //取得请求内容     
  51.             HttpEntity entity = response.getEntity();     
  52.             //显示内容     
  53.             if (entity != null) {     
  54.                 // 显示结果     
  55.                      
  56.                 System.out.println(EntityUtils.toString(entity,"utf-8"));     
  57.                      
  58.             }     
  59.             if (entity != null) {     
  60.                 entity.consumeContent();     
  61.             }     
  62.         }     
  63.     }     
  64. }    

猜你喜欢

转载自sleeply520.iteye.com/blog/1817839