HttpClient的java Set静态代理
我想写一个测试来测试我的服务,它使用HttpClient
发送GET请求
service.logger.info("Testing GET request with param= " + "test");
service.logger.info(service.getSuggestions(searchTerms1));
service.logger.info("Testing GET request with param= " + "weibo");
service.logger.info(service.getSuggestions(searchTerms2));
当我在公司的网络中运行这个程序时,它不工作,因为它使用代理
我不想更改我服务的现有代码,所以我希望找到一种在外部更改set proxy的方法,我的HTTP客户端将使用该代理发送请求。有办法吗?我正在使用eclipse
下面是我如何发送请求以从网络获取结果
private final String getResultFromURL(String url) throws ClientProtocolException, IOException {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1 * 1000).build();
HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", USER_AGENT_MOZILLIA);
HttpResponse response = client.execute(request);
StringBuffer result = new StringBuffer();
try(BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()))){
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
finally{
request.releaseConnection();
EntityUtils.consume(response.getEntity());
}
logger.debug("----result from URL:"+url +" "+result);
return result.toString();
}
# 1 楼答案
在java_选项中设置代理,如下所示
在窗户里
在*尼克斯
# 2 楼答案
你可以用命令行来做
https://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html