httpclient 使用代理

    代理ip的原理是: 把A机器把请求request发送到代理服务器C, C在向服务器B发送request, 然后通过C把响应response给A

在使用httpclient 写爬虫程序的时候通常 需要使用代理:

public class HttpClientProxyTest {
	
	@Test
	public void test1() throws ClientProtocolException, IOException {
		HttpClient httpclient = getClient(true);
		String url = "http://1111.ip138.com/ic.asp";
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse res = (CloseableHttpResponse) httpclient.execute(httpGet);
		String string = EntityUtils.toString(res.getEntity(), "gb2312");
		System.out.println(string);
	}
	
	private HttpClient getClient(boolean proxy) {
		if(proxy) {
			// 阿里的代理服务器
			String hostStr = "42.121.105.155";
			HttpHost host = new HttpHost(hostStr, 8888);
			DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
				host);
			return HttpClients.custom()
				.setRoutePlanner(routePlanner).build();
		} else {
			return HttpClients.custom().build();
		}
	}

}


程序输出:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> 您的IP地址 </title>
</head>
<body style="margin:0px"><center>您的IP是:[42.121.105.155] 来自:浙江省杭州市 阿里巴巴</center></body></html>


如果将代理去掉即调用: getClient(false) 输出如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title> 您的IP地址 </title>
</head>
<body style="margin:0px"><center>您的IP是:[211.157.164.1] 来自:北京市 263网络通信</center></body></html>






Java使用HttpClient进行代理访问百度,你可以按照以下步骤进行: 1. 创建一个HttpClient实例。 2. 配置HttpClient使用代理服务器。 3. 创建HttpGet或者HttpPost请求实例,并指定要访问的百度URL。 4. 执行请求,并获取HttpResponse。 5. 从HttpResponse中获取响应内容。 以下是一个使用Java HttpClient通过代理访问百度的示例代码: ```java import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpProxyExample { public static void main(String[] args) throws Exception { // 创建HttpClient实例 CloseableHttpClient httpClient = HttpClients.custom().build(); // 创建HttpGet请求实例,并指定百度的URL HttpGet httpGet = new HttpGet("http://www.baidu.com"); // 配置代理服务器,这里假设代理服务器地址为127.0.0.1,端口为8888 httpClient.getParams().setParameter("http.proxyHost", "127.0.0.1"); httpClient.getParams().setParameter("http.proxyPort", 8888); // 可以选择设置代理的用户名和密码(如果需要的话) // httpClient.getParams().setParameter("http.proxyUser", "username"); // httpClient.getParams().setParameter("http.proxyPassword", "password"); // 执行请求 CloseableHttpResponse response = httpClient.execute(httpGet); // 获取响应实体内容 HttpEntity entity = response.getEntity(); // 打印响应内容到控制台 System.out.println(EntityUtils.toString(entity)); // 关闭连接 EntityUtils.consume(entity); httpClient.close(); } } ``` 注意:在实际应用中,代理服务器地址、端口以及可能的用户名和密码应该根据你所使用代理服务器来设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值