HttpClient代理IP及设置连接读取超时
1、不废话,上代码:
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpClient实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com"); // 创建Httpget实例
//设置Http报文头信息
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");
HttpHost httoHost = new HttpHost("113.2.63.7",8118);//代理Ip
//RequestConfig requestConfig = RequestConfig.custom().setProxy(httoHost).build();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000)//设置连接超时时间
.setSocketTimeout(10000)//设置读取超时时间
.setProxy(httoHost)//设置代理
.build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = null;
response = httpClient.execute(httpGet); // 执行http get请求
HttpEntity httpEntity = response.getEntity(); // 获取返回实体
String web = EntityUtils.toString(httpEntity, "utf-8"); // 解析实体
System.out.println("网页内容是:");
System.out.println(web);
response.close(); // 关闭response
httpClient.close(); // 关闭HttpClient实体
}
2、关于IP代理
IP代理分为:透明代理、匿名代理、混淆代理、高匿代理
1)透明代理(Transparent Proxy)
REMOTE_ADDR = Proxy IP
HTTP_VIA = Proxy IP
HTTP_X_FORWARDED_FOR = Your IP
透明代理虽然可以直接“隐藏”你的IP地址,但是还是可以从HTTP_X_FORWARDED_FOR来查到你是谁。
2)匿名代理(Anonymous Proxy)
REMOTE_ADDR = proxy IP
HTTP_VIA = proxy IP
HTTP_X_FORWARDED_FOR = proxy IP
匿名代理比透明代理进步了一点:别人只能知道你用了代理,无法知道你是谁。
还有一种比纯匿名代理更先进一点的:混淆代理,见下节。
3)混淆代理(Distorting Proxies)
REMOTE_ADDR = Proxy IP
HTTP_VIA = Proxy IP
HTTP_X_FORWARDED_FOR = Random IP address
如上,与匿名代理相同,如果使用了混淆代理,别人还是能知道你在用代理,但是会得到一个假的IP地址,伪装的更逼真:-)
4)高匿代理(Elite proxy或High Anonymity Proxy)
REMOTE_ADDR = Proxy IP
HTTP_VIA = not determined
HTTP_X_FORWARDED_FOR = not determined
可以看出来,高匿代理让别人根本无法发现你是在用代理,所以是最好的选择。
Reference:
[1]Java1234_小锋, HttpClient使用代理IP, http://blog.java1234.com/blog/articles/251.html
[2]Java1234_小锋, HttpClient连接超时及读取超时, http://blog.java1234.com/blog/articles/254.html