1.创建httpclient
public static void main(String[] args) throws ClientProtocolException, IOException {
//创建httpclient实例
CloseableHttpClient httpClient=HttpClients.createDefault();
//创建httpGets实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");
//模拟浏览器访问
httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
//执行http get请求
CloseableHttpResponse response = httpClient.execute(httpGet);
//获取响应状态 Status
System.out.println("Status:"+response.getStatusLine().getStatusCode());
//获取返回实体
HttpEntity entity=response.getEntity();
//获取响应的内容类型Content-Type
System.out.println("Content-Type:"+entity.getContentType().getValue());
//获取网页内容
//System.out.println("网页内容"+EntityUtils.toString(entity, "utf-8"));
//关闭流
response.close();
//httpClient关闭
httpClient.close();
}
2.处理图片
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient=HttpClients.createDefault();//创建httpclient实例
HttpGet httpGet = new HttpGet("http://www.java1234.com/gg/sxt10.jpg");//创建httpGets实例
//模拟浏览器访问
httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
CloseableHttpResponse response = httpClient.execute(httpGet);//执行http get请求
HttpEntity entity=response.getEntity();//获取返回实体
if(entity!=null) {
//获取图片
System.out.println("ContentType:"+entity.getContentType().getValue());
InputStream inputStream=entity.getContent();
//FileUtils(Apache Commons IO包下面的)
FileUtils.copyToFile(inputStream, new File("D://lele.jpg"));
}
//System.out.println("网页内容"+EntityUtils.toString(entity, "utf-8"));//获取网页内容
response.close();//关闭流
httpClient.close();//httpClient关闭
}
3.代理ip
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpClient=HttpClients.createDefault();//创建httpclient实例
HttpGet httpGet = new HttpGet("http://www.tuicool.com");//创建httpGets实例
//代理ip网站https://www.xicidaili.com/
HttpHost proxy = new HttpHost("61.135.217.7",80);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
httpGet.setConfig(config);
//模拟浏览器访问
httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
CloseableHttpResponse response = httpClient.execute(httpGet);//执行http get请求
HttpEntity entity=response.getEntity();//获取返回实体
System.out.println("网页内容"+EntityUtils.toString(entity, "utf-8"));//获取网页内容
response.close();//关闭流
httpClient.close();//httpClient关闭
}