在爬取数据的时候有的时候不能使用自己的真实ip爬取,需要简单配置一下用代理来完成。
public static String getHttpGet(String url){ //get请求
HttpHost proxy = new HttpHost("127.0.0.1", 1080);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
HttpGet get=new HttpGet(url);
try {
CloseableHttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}