httpclient

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关闭
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java程序员劝退师

感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值