httpclient默认不使用代理,如果本机使用了代理,httpclient需要设置代理服务器
@Test
public void test(){
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.google.com");
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
.setProxy(new HttpHost("localhost", 19180))
.build();
httpGet.setConfig(requestConfig);
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200 ){
String jsonStr = EntityUtils.toString(response.getEntity());
System.out.println(jsonStr);
}else{
System.out.println("请求失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}