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 周銳 閱讀(4473) 評論(0)  編輯  收藏 所屬分類: ApacheJava
          主站蜘蛛池模板: 英吉沙县| 云阳县| 武隆县| 神木县| 内丘县| 元阳县| 三门县| 和硕县| 西平县| 罗城| 墨竹工卡县| 巫山县| 姜堰市| 莱阳市| 镇康县| 乳源| 偏关县| 阿鲁科尔沁旗| 台前县| 红河县| 德兴市| 康平县| 金坛市| 拉萨市| 镇宁| 衡山县| 泰顺县| 沙河市| 白朗县| 于都县| 铁岭县| 晴隆县| 漠河县| 沧源| 郯城县| 萨嘎县| 于田县| 温州市| 南漳县| 双桥区| 汉中市|