背景:
我們有個車管系統,需要定期的去查詢車輛的違章,之前一直是調第三方接口去查,后面發現數據不准確(和深圳交警查的對不上),問題比較多。於是想干脆直接從深圳交警上查,那不就不會出問題了嗎,但是問題又來了,因為車比較多,一次查的數據量很大,如果同時間段大批量請求深圳交警可能會導致ip被他們那邊封禁,那有什么解決辦法呢?
解決方案:
網上查了很多資料,大致講的就是設置代理ip,然后通過代理ip去訪問。
設置代理ip:
設置代理ip的方式有很多種,我這里講其中一種,通過httpClient設置代理Ip,httpClient我想大家都不陌生了吧,它幾乎封裝了所有的http請求方法,當然其中也提供了設置代理ip的方法,廢話不多說,直接上代碼。
@Test
public void test() throws Exception{
//創建httpClient實例
CloseableHttpClient httpClient = HttpClients.createDefault();
//創建httpGet實例
HttpPost httpPost = new HttpPost("請求地址");
//設置代理IP,設置連接超時時間 、 設置 請求讀取數據的超時時間 、 設置從connect Manager獲取Connection超時時間、
HttpHost proxy = new HttpHost("125.77.49.244", 808);
List<NameValuePair> params = new ArrayList<NameValuePair>(); //設置請求參數
params.add(new BasicNameValuePair("car_number", "粵B123456"));
params.add(new BasicNameValuePair("licensePlateNo", "粵B123456"));
HttpEntity entitys = new UrlEncodedFormEntity(params, "UTF-8");
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxy)
.setConnectTimeout(10000)
.setSocketTimeout(10000)
.setConnectionRequestTimeout(3000)
.build();
httpPost.setConfig(requestConfig);
httpPost.setEntity(entitys);
//設置請求頭消息
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
CloseableHttpResponse response = httpClient.execute(httpPost);
if (response != null){
HttpEntity entity = response.getEntity(); //獲取返回實體
if (entity != null){
System.out.println("網頁內容為:"+ EntityUtils.toString(entity,"utf-8"));
}
}
if (response != null){
response.close();
}
if (httpClient != null){
httpClient.close();
}
}
這里講一下另外一種設置代理ip的方案,直接上代碼
@Test
public void test3() throws Exception{
System.getProperties().setProperty("proxySet","true");
System.getProperties().setProperty("http.proxyHost","60.191.201.38"); //設置ip
System.getProperties().setProperty("http.proxyPort","45461"); //設置端口號
Document cocument = Jsoup.connect("http://127.0.0.1:8082/newAtb/indexData.do").ignoreContentType(true).post();
}
至於代理ip,有很多種獲取方式,大部分需要花錢買,免費的很少,這里給大家一個網站 http://www.xicidaili.com/,這個網站每天都會更新免費的代理ip,我們可以定期去上面爬取數據到本地供我們使用,這里貼上一段本人爬取ip的代碼
public void setProxyIpList(){
try {
String rLine;
URL url = new URL("http://www.xicidaili.com/");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");
List<String> list = new ArrayList<>();
InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream(),"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int a = 0;
do{
rLine = bufferedReader.readLine();
String reg1 = "\\d+"; //匹配端口號
String reg = "\\d+\\.\\d+\\.\\d+\\.\\d+"; //匹配ip
if(rLine.contains("<td>") ) {
rLine = rLine.substring(rLine.indexOf("<td>") + 4, rLine.indexOf("</td>"));
if (Pattern.matches(reg, rLine) || Pattern.matches(reg1, rLine)) {
list.add(rLine);
a++;
}
}
}while (a !=40);
for (int i=0; i< list.size();i=i+2 ) {
IPort ip = new IPort();
ip.setIp(list.get(i));
ip.setPort(Integer.parseInt(list.get(i+1)));
listPort.add(ip);
}
} catch (Exception e) {
e.printStackTrace();
logger.info("拉取代理ip異常:" + e.getMessage());
}
}
