现在用gae也时常不稳定,处于基本报废状态。今天找到了另外一个可用的,并且发现httpclient也有自带的设置代理功能。代码如下:
HttpHost proxy = new HttpHost("127.0.0.1", 8087); DefaultProxyRoutePlanner routePlanner = newDefaultProxyRoutePlanner(proxy); CloseableHttpClient httpClient =HttpClients.custom().setRoutePlanner(routePlanner).build();
与通常的client初始化不一样,在httpclient初始化之前,需要new一个httpHost对象,并设置代理地址及端口号。根据代理而定。构造参数两个,第一个设置代理服务器地址,此处为将请求返回本地处理。第二个为端口号。此方法可将该client所有连接使用代理访问.
CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build(); HttpHost proxy = newHttpHost("127.0.0.1", 8087); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpPost httpPost = new HttpPost("url"); httpPost.setConfig(config); //CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
用如上方法可以对指定请求使用代理。