HttpClient代理IP及设置连接读取超时

博客内容介绍了HttpClient中代理IP的类型,包括透明代理、匿名代理、混淆代理和高匿代理,详细解释了各类代理的工作原理,并强调了高匿代理在保护用户IP方面的优势。同时,提到了设置连接和读取超时的重要性。
摘要由CSDN通过智能技术生成


1、不废话,上代码:

public static void main(String[] args) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建HttpClient实例
		HttpGet httpGet = new HttpGet("http://www.tuicool.com"); // 创建Httpget实例
		//设置Http报文头信息
		httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0");
		HttpHost httoHost = new HttpHost("113.2.63.7",8118);//代理Ip
		//RequestConfig requestConfig = RequestConfig.custom().setProxy(httoHost).build();
		RequestConfig requestConfig = RequestConfig.custom()
				.setConnectTimeout(10000)//设置连接超时时间
				.setSocketTimeout(10000)//设置读取超时时间
				.setProxy(httoHost)//设置代理
				.build();
		httpGet.setConfig(requestConfig);
		CloseableHttpResponse response = null;
		response = httpClient.execute(httpGet); // 执行http get请求
		HttpEntity httpEntity = response.getEntity(); // 获取返回实体
		String web = EntityUtils.toString(httpEntity, "utf-8"); // 解析实体
		System.out.println("网页内容是:");
		System.out.println(web);
		response.close(); // 关闭response
		httpClient.close(); // 关闭HttpClient实体
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值