我正在努力解决问题,因为现在大约两天,似乎没有找到解决方案。
为了改进对我们产品中系统代理设置的支持,我编写了一个测试应用程序,对一个小型http代理服务器进行一些测试,我在一个docker镜像中运行。
一方面,我与普通的URL.openStream()进行连接...哪个工作得很好。它识别我的开发人员盒设置,我指向docker run squid或tinyproxy,它可以从网上下载文件。
我使用httpclient 3.X和4.X进行相同的测试。当连接到两个代理时,两者都失败并出现超时错误。 由于两者具有相同的行为,我只需选择我的httpclient 4.x配置来显示:
public void testDownloadWithHTTPClient4() throws ClientProtocolException, IOException {
System.out.println("DOWNLOADTEST httpclient 4.x");
RequestConfig config = RequestConfig.custom().setSocketTimeout(TIMEOUT * 1000)
.setConnectTimeout(TIMEOUT * 1000).setConnectionRequestTimeout(TIMEOUT * 1000).build();
CloseableHttpClient httpclient = HttpClientBuilder.create()
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
.setDefaultRequestConfig(config).build();
try {
HttpGet httpget = new HttpGet(DOWNLOADURL);
System.out.println("Executing request " + httpget.getRequestLine());
ResponseHandler<Boolean> resStreamHandler = new ResponseHandler<Boolean>() {
@Override
public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
String currentDirectory = new java.io.File(".").getCanonicalPath();
File destinationFile = new File(currentDirectory, myfile.war");
FileUtils.copyInputStreamToFile(entity.getContent(), destinationFile);
return true;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
}
};
httpclient.execute(httpget, resStreamHandler);
} finally {
httpclient.close();
}
}
我之前也打过电话
public void prepareProxysettings() {
// try to get system preferences for proxy-settings
Properties props = System.getProperties();
props.setProperty("java.net.useSystemProxies", "true");
}
结果是,客户端似乎认识到要使用代理,但随后失败并出现以下异常:
org.apache.http.conn.ConnectTimeoutException: Connect to 172.16.7.48:6666 [/172.16.7.48] failed: Connect timed out
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:132)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:371)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
[...]
Caused by: java.net.SocketTimeoutException: Connect timed out
at java.net.SocksSocketImpl.readSocksReply(SocksSocketImpl.java:125)
现在我被困住了,不知道该怎么做。显然,代理设置并没有错,因为我的普通URL下载工作。此外,httpclient还认识到需要拥有代理。但为什么它发送一个请求,这两个不同的http代理都不理解?
非常感谢任何帮助!
答案 0 :(得分:1)
最后我想我发现了,我的设置出了什么问题。 愚蠢的我,配置我的开发人员桌面(win7)使用我的testproxy。但是我保留了默认行为,就像将此代理用于所有可能的连接类型一样。如果我这样做,我会用httpclients获得描述的timouets。
只要我为http-connections配置代理,只有所有下载工作方式都能很好地工作并且tinyproxy的日志证明,我的连接是通过代理连接的。