3.2.7 HttpClient代理服务器的使用

    技术2022-07-10  124

    在3.2.6节中,已经介绍了RequestConfig类来进行超时时间的设置,本节将直接使用该类来配置代理服务器进行访问,程序3-19为针对实例化的请求方法配置代理服务器。

    //程序3-19 public class HttpClientProxy { public static void main(String[] args) throws Exception { //实例化httpClient HttpClient httpClient = HttpClients.custom().build(); //设置代理 HttpHost proxy = new HttpHost("117.63.135.139",29395, null); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet httpGet = new HttpGet("https://searchcustomerexperience.techtarget.com/info/news"); //针对实例化的请求方法设置代理 httpGet.setConfig(config); HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200){ String result = EntityUtils.toString(httpResponse.getEntity(),"utf-8"); System.out.println(result); } } }
    Processed: 0.013, SQL: 12