posts - 431,  comments - 344,  trackbacks - 0
  1. import java.io.BufferedReader;   
  2. import java.io.InputStreamReader;   
  3. import org.apache.http.HttpEntity;   
  4. import org.apache.http.HttpHost;   
  5. import org.apache.http.HttpResponse;   
  6. import org.apache.http.auth.AuthScope;   
  7. import org.apache.http.auth.UsernamePasswordCredentials;   
  8. import org.apache.http.client.methods.HttpGet;   
  9. import org.apache.http.conn.params.ConnRoutePNames;   
  10. import org.apache.http.impl.client.DefaultHttpClient;   
  11.   
  12. /**  
  13.  * HttpClient 4.0通过代理访问Https的代码例子。  
  14.  *   
  15.  * @author JAVA世纪网(java2000.net, laozizhu.com)  
  16.  */  
  17. public class HttpsProxyGet {   
  18.   public static void main(String[] args) throws Exception {   
  19.     DefaultHttpClient httpclient = new DefaultHttpClient();   
  20.     // 认证的数据   
  21.     // 我这里是瞎写的,请根据实际情况填写   
  22.     httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20"8080),   
  23.         new UsernamePasswordCredentials("username""password"));   
  24.     // 访问的目标站点,端口和协议   
  25.     HttpHost targetHost = new HttpHost("www.google.com"443"https");   
  26.     // 代理的设置   
  27.     HttpHost proxy = new HttpHost("10.60.8.20"8080);   
  28.     httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);   
  29.     // 目标地址   
  30.     HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");   
  31.     System.out.println("目标: " + targetHost);   
  32.     System.out.println("请求: " + httpget.getRequestLine());   
  33.     System.out.println("代理: " + proxy);   
  34.     // 执行   
  35.     HttpResponse response = httpclient.execute(targetHost, httpget);   
  36.     HttpEntity entity = response.getEntity();   
  37.     System.out.println("----------------------------------------");   
  38.     System.out.println(response.getStatusLine());   
  39.     if (entity != null) {   
  40.       System.out.println("Response content length: " + entity.getContentLength());   
  41.     }   
  42.     // 显示结果   
  43.     BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));   
  44.     String line = null;   
  45.     while ((line = reader.readLine()) != null) {   
  46.       System.out.println(line);   
  47.     }   
  48.     if (entity != null) {   
  49.       entity.consumeContent();   
  50.     }   
  51.   }   
  52. }  
posted on 2009-07-30 13:50 周锐 阅读(4448) 评论(0)  编辑  收藏 所属分类: ApacheJava

只有注册用户登录后才能发表评论。


网站导航: