(HttpClient技术)Httpclient如何设置代理IP和端口(Port)
前言
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