// 1.创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 2.HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
RequestConfig defaultRequestConfig = RequestConfig.custom().build();
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setProxy(new HttpHost("web-proxy.houston.hp.com", 8080)).build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
try {
// 3.执行get请求
HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
// 4.获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
// 响应状态
System.out.println("status:" + httpResponse.getStatusLine());
// 判断响应实体是否为空
if (entity != null) {
// System.out.println("contentEncoding:" +
// entity.getContentEncoding());
// System.out.println("response content:" +
// EntityUtils.toString(entity));
response = EntityUtils.toString(entity);
return response;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭流并释放资源
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return response;