在访问一个网站时,有时我们不想让对方获取到我们的真实IP,这种情况下,就可以使用代理IP进行访问。
1、maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
2、使用代理IP实例
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpProxy {
public static void main(String[] args) throws Exception {
// 创建HttpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建Httpget实例 ,http://2018.ip138.com/ic.asp为该网址返回对应的ip
//以下为要访问的网址
HttpGet httpGet = new HttpGet("http://budwuv.v.vote8.cn");
//代理IP设置,代理 ip查询地址:https://www.xicidaili.com/
HttpHost httpHost = new HttpHost("112.87.68.95",9999);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000)//设置连接超时时间,单位毫秒
.setSocketTimeout(10000)//设置读取超时时间,单位毫秒
.setProxy(httpHost)//设置代理
.build();
httpGet.setConfig(requestConfig);
//设置Http报文头信息
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response = httpClient.execute(httpGet); // 执行http get请求
if (response != null){
HttpEntity entity = response.getEntity(); //获取返回实体
if (entity != null){
System.out.println("网页内容为:");
System.out.println(EntityUtils.toString(entity,"gbk"));
}
}
if (response != null){
response.close();
}
if (httpClient != null){
httpClient.close();
}
}
}
3、代理IP获取方式
访问 http://www.xicidaili.com/,可在该网站上爬取最新的高匿代理IP,保存到本地;当一个IP被屏蔽或获取连接超时时,取出下一个IP,当本地IP数不足时再重新爬取,以此类推。
4、验证举例
以网上在线投票系统为例,在PC端访问“投票吧”创建投票活动,设置为“相同IP不允许不能在30分钟内重复投票”,发布投票活动http://budwuv.v.vote8.cn。
正常访问投票网站,核心代码如下:
HttpGet httpGet = new HttpGet("http://budwuv.v.vote8.cn");
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000)//设置连接超时时间,单位毫秒
.setSocketTimeout(10000)//设置读取超时时间,单位毫秒
.build();
httpGet.setConfig(requestConfig);
//设置Http报文头信息
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response = httpClient.execute(httpGet); // 执行http get请求
页面截图如下:
用设置代理的方式重新访问该网站,核心代码如下:
HttpGet httpGet = new HttpGet("http://budwuv.v.vote8.cn");
//代理IP设置
HttpHost httpHost = new HttpHost("112.87.68.95",9999);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(10000)//设置连接超时时间,单位毫秒
.setSocketTimeout(10000)//设置读取超时时间,单位毫秒
.setProxy(httpHost)//设置代理
.build();
httpGet.setConfig(requestConfig);
//设置Http报文头信息
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response = httpClient.execute(httpGet); // 执行http get请求
该页面可以正常投票,截图如下: