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 周銳 閱讀(4480) 評論(0)  編輯  收藏 所屬分類: Apache 、Java
          主站蜘蛛池模板: 陇川县| 镇安县| 安国市| 宜章县| 沁水县| 米泉市| 二连浩特市| 建水县| 綦江县| 行唐县| 清水河县| 东台市| 进贤县| 罗江县| 金川县| 临颍县| 上蔡县| 商南县| 绵阳市| 吴川市| 文昌市| 临邑县| 敖汉旗| 六盘水市| 英吉沙县| 宜君县| 中宁县| 阿克苏市| 沁水县| 新邵县| 霍山县| 富顺县| 青河县| 合肥市| 凤庆县| 浮梁县| 洞口县| 雷波县| 于田县| 兴国县| 获嘉县|