注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
Java使用httpclient调用Http代理IP的代码样例
站大爷 官方 2024-01-20

温馨提示:

1.   此代码样例同时支持访问http和https网页

2.   使用用户名密码访问的情况下,每次请求httpclient会发送两次进行认证从而导致请求耗时增加,建议使用终端IP授权访问

3.   若有多个用户名、密码进行认证,需要在代码中添加AuthCacheValue.setAuthCache(new AuthCacheImpl());

4.   依赖包下载:

     httpclient-4.5.6.jar

     httpcore-4.4.10.jar

     commons-codec-1.10.jar

     commons-logging-1.2.jar


import java.net.URL;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
* 使用httpclient请求代理服务器 请求http和https网页均适用
*/
public class TestProxyHttpClient {

	private static String pageUrl = "https://example.com"; // 要访问的目标网页
	private static String proxyIp = "159.138.141.125"; // 代理服务器IP
	private static int proxyPort = 13916; // 端口号
	// 用户名密码授权
	private static String username = "username";
	private static String password = "password";

	public static void main(String[] args) throws Exception {
		// JDK 8u111版本后,目标页面为HTTPS协议,启用proxy用户密码鉴权
		System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope(proxyIp, proxyPort),
				new UsernamePasswordCredentials(username, password));
		CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
		try {
			URL url = new URL(pageUrl);
			HttpHost target = new HttpHost(url.getHost(), url.getDefaultPort(), url.getProtocol());
			HttpHost proxy = new HttpHost(proxyIp, proxyPort);

			/*
			httpclient各个版本设置超时都略有不同, 此处对应版本4.5.6
			setConnectTimeout:设置连接超时时间
			setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间
			setSocketTimeout:请求获取数据的超时时间
			*/
			RequestConfig config = RequestConfig.custom().setProxy(proxy).setConnectTimeout(6000)
							.setConnectionRequestTimeout(2000).setSocketTimeout(6000).build();
			HttpGet httpget = new HttpGet(url.getPath());
			httpget.setConfig(config);
			httpget.addHeader("Accept-Encoding", "gzip"); // 使用gzip压缩传输数据让访问更快
			httpget.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36");
			CloseableHttpResponse response = httpclient.execute(target, httpget);
			try {
				System.out.println(response.getStatusLine());
				System.out.println(EntityUtils.toString(response.getEntity()));
			} finally {
				response.close();
			}
		} finally {
			httpclient.close();
		}
	}
}


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品