我在我的系统属性中设置了代理。在我的 java 应用程序中,使用 Apache HttpClient 将文件 HttpPut 到 URL,代码如下。我尝试创建客户端的所有 3 种方法都返回 404。当我没有代理设置时,这同样适用于 HttpClients.getDefault()。我该如何解决?感谢你的帮助。谢谢
public void sendHTTPPutRequest(@NotNull HttpPut req)
throws DataExtractException {
try (CloseableHttpClient client = buildClient(req.getURI());
CloseableHttpResponse response = client.execute(req)) {
// ........
}
}
private CloseableHttpClient buildClient(URI uri) {
Proxy firstProxy = ProxySelector.getDefault().select(uri).get(0);
InetSocketAddress proxyAddress = (InetSocketAddress) firstProxy.address();
HttpHost proxy = (proxyAddress == null || firstProxy.equals(Proxy.NO_PROXY)) ? null
: new HttpHost(proxyAddress.getHostName(), proxyAddress.getPort());
BasicCookieStore cookieStore = new BasicCookieStore();
return HttpClients.custom().setDefaultCookieStore(cookieStore).setProxy(proxy).build();
// NOTE: Here I also tried HttpClients.createSystem(), and HttpClients.getDefault()
// all 3 clients returned 404.
}