前言
Httpclient 3.X和Httpclient 4.X如何設置代理呢?現在的網絡比較成熟,各種大型網站爲了防止別人惡意攻擊自己的網站,都會對訪問者的IP進行限制,所以爲了能夠多次訪問一個網站,Httpclient在使用過程就會遇到設置代理的問題,那麼如何來給Httpclient設置代理IP呢?
Httpclient 3.X和Httpclient 4.X的版本差距比較大,設置代理的方式各不一樣.
Httpclient 3.X 設置代理的方式:
//設置不帶用戶和密碼的代理
httpClient.getHostConfiguration().setProxy(ip, port) ;
//設置帶用戶和密碼的代理
if(ip!=null && !ip.equals("")){
System.out.println("使用代理:"+ip+"--"+port+"--"+httpUsername+"--"+httpPwd);
httpClient.getHostConfiguration().setProxy(ip, port) ;
if (StringUtils.isNotBlank(httpUsername)) {
UsernamePasswordCredentials upc = null;
upc = new UsernamePasswordCredentials(httpUsername, httpPwd);
httpClient.getState().setProxyCredentials(AuthScope.ANY, upc);
}
}
Httpclient 4.X設置代理的方式:
if(ip!=null && !ip.equals("")){
HttpHost proxy = new HttpHost(ip, port);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
}
Httpclient使用代理訪問網頁的例子:
給大家分享一個使用代理查詢對IP訪問限制的網站,希望有所幫助!
public class Xicidaili {
public static void get(DefaultHttpClient httpClient, String ip, int port) {
boolean re = false;
if (ip != null && !ip.equals("")) {
HttpHost proxy = new HttpHost(ip, port);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
proxy);
}
if (httpClient == null) {
httpClient = HttpClientUtuils.handleNewHttpClient(120000, 120000);
}
String sg1 = "";
HttpGet g1 = null;
try {
g1 = new HttpGet("https://www.xicidaili.com/nn/");
HttpResponse response2 = httpClient.execute(g1);
sg1 = EntityUtils.toString(response2.getEntity());
if(response2.getStatusLine().getStatusCode() == 200 && sg1.indexOf("<table id=\"ip_list\">")!=-1
&& sg1.indexOf("<tr class")!=-1) {
sg1 = sg1.substring(sg1.indexOf("<table id=\"ip_list\">")) ;
while(sg1.indexOf("<tr class")!=-1) {
sg1 = sg1.substring(sg1.indexOf("<tr class")) ;
String trstring = sg1.substring(0, sg1.indexOf("</tr>")) ;
sg1 = sg1.substring(sg1.indexOf("</tr>")+4) ;
if(trstring.indexOf("<td class=\"country\">")!=-1) {
trstring = trstring.substring(trstring.indexOf("<td>")+4) ;
String ips = trstring.substring(0, trstring.indexOf("</td>")).trim() ;
trstring = trstring.substring(trstring.indexOf("<td>")+4) ;
String ports = trstring.substring(0, trstring.indexOf("</td>")).trim() ;
String https = ips + ":" + ports;
System.out.println(https);
}
}
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
}
sg1 = null;
System.gc();
return;
}
/**
* @param args
*/
public static void main(String[] args) {
Xicidaili is_address_right_hm = new Xicidaili();
is_address_right_hm.get(null, "", 0, null);
}
}
如果需要了解更多技術,請訪問個人博客:http://www.tech58.net