HttpClient使用代理访问

使用代理ip,端口访问目标网址。 

 

RequestConfig config = null;
//使用代理

if(null != proxy && StringUtils.isNotBlank(proxy.ip) && proxy.port > 0){
    HttpHost proxy = new HttpHost(proxy.ip, proxy.port);  
    config = RequestConfig.custom().setProxy(proxy).build(); 
}else{
//没有代理,使用默认值
    config = RequestConfig.custom().build();
}

HttpGet hg = new HttpGet(url);
//设置代理
hg.setConfig(config);

 

posted @ 2016-10-25 16:24  swbzmx  阅读(7102)  评论(0编辑  收藏  举报