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)  編輯  收藏 所屬分類: ApacheJava
          主站蜘蛛池模板: 枣阳市| 名山县| 竹山县| 文山县| 泌阳县| 垫江县| 辽中县| 寿宁县| 雷州市| 亚东县| 翼城县| 吐鲁番市| 陵川县| 宿迁市| 曲阳县| 太保市| 云南省| 阿坝县| 垦利县| 博野县| 自贡市| 佳木斯市| 科尔| 新宁县| 旬阳县| 巴东县| 台南市| 高安市| 蓝田县| 静海县| 丹阳市| 深圳市| 昌吉市| 包头市| 儋州市| 左贡县| 沧源| 拜城县| 富平县| 石渠县| 贞丰县|