HttpClient使用代理ip

来源:互联网 发布:枪神纪刷枪软件手机版 编辑:程序博客网 时间:2024/06/09 23:25
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();//创建HttpClient实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");//创建HttpGet实例
HttpHost proxy = new HttpHost("121.232.147.232",9000);//代理ip地址.端口
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(config);//设置代理ip 可根据响应状态进行换ip,ip在代理ip网站进行抓取,抓取到放到队列中
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0");//设置请求头信息
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();//获取返回实体
System.out.println("网页内容:"+EntityUtils.toString(entity, "utf-8"));
response.close();//response闭关
httpClient.close();//httpClient关闭
}
0 0