//创建时
public static HttpClient getHttpClient() {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("代理服务器", 代理服务器端口), // 代理服务器信息
new UsernamePasswordCredentials(账号, 密码)); // 代理服务器账号密码
CloseableHttpClient httpclient = HttpClients.custom() // 构建需要认证的httpclient
.setDefaultCredentialsProvider(credsProvider).build();
return httpclient;
}
//使用时
public void start() throws Exception {
String url = "http://xxx/list.html?cat=9987,653,655&page=1";
HttpHost proxy = new HttpHost("代理服务器", 代理服务器端口);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet httpget = new HttpGet(url);
httpget.setConfig(config);
HttpClient httpClient = ApiService.getHttpClient();
CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpget);
String html = EntityUtils.toString(response.getEntity(), "UTF-8");//获取响应
Document document = Jsoup.parse(html);
String text = document.select("#XXX").text();
System.out.println(text);
}