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世紀(jì)網(wǎng)(java2000.net, laozizhu.com)  
          16.  */  
          17. public class HttpsProxyGet {   
          18.   public static void main(String[] args) throws Exception {   
          19.     DefaultHttpClient httpclient = new DefaultHttpClient();   
          20.     // 認(rèn)證的數(shù)據(jù)   
          21.     // 我這里是瞎寫的,請(qǐng)根據(jù)實(shí)際情況填寫   
          22.     httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20"8080),   
          23.         new UsernamePasswordCredentials("username""password"));   
          24.     // 訪問的目標(biāo)站點(diǎn),端口和協(xié)議   
          25.     HttpHost targetHost = new HttpHost("www.google.com"443"https");   
          26.     // 代理的設(shè)置   
          27.     HttpHost proxy = new HttpHost("10.60.8.20"8080);   
          28.     httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);   
          29.     // 目標(biāo)地址   
          30.     HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");   
          31.     System.out.println("目標(biāo): " + targetHost);   
          32.     System.out.println("請(qǐng)求: " + httpget.getRequestLine());   
          33.     System.out.println("代理: " + proxy);   
          34.     // 執(zhí)行   
          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.     // 顯示結(jié)果   
          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 周銳 閱讀(4472) 評(píng)論(0)  編輯  收藏 所屬分類: ApacheJava
          主站蜘蛛池模板: 新津县| 潮安县| 定边县| 云浮市| 尚志市| 襄垣县| 沅陵县| 嘉峪关市| 晋江市| 柳州市| 聂拉木县| 天水市| 卫辉市| 岑巩县| 龙里县| 筠连县| 琼海市| 安陆市| 高州市| 祁门县| 扎鲁特旗| 武乡县| 宾阳县| 百色市| 连江县| 历史| 九寨沟县| 宜都市| 喀什市| 滦南县| 宝兴县| 哈密市| 海门市| 吴川市| 贞丰县| 隆昌县| 上思县| 甘孜县| 张掖市| 墨玉县| 清原|