import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* HttpClient 4.0通過代理訪問Https的代碼例子。
*
* @author JAVA世紀網(java2000.net, laozizhu.com)
*/
public class HttpsProxyGet {
public static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
// 認證的資料
// 我這裡是瞎寫的,請根據實際情況填寫
httpclient.getCredentialsProvider().setCredentials(new AuthScope("10.60.8.20", 8080),
new UsernamePasswordCredentials("username", "password"));
// 訪問的目標網站,連接埠和協議
HttpHost targetHost = new HttpHost("www.google.com", 443, "https");
// 代理的設定
HttpHost proxy = new HttpHost("10.60.8.20", 8080);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
// 目標地址
HttpGet httpget = new HttpGet("/adsense/login/zh_CN/?");
System.out.println("目標: " + targetHost);
System.out.println("請求: " + httpget.getRequestLine());
System.out.println("代理: " + proxy);
// 執行
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
// 顯示結果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
}
}