我想在URL上发送HttpClient
请求之前设置代理。因为我能够连接它,所以curl命令可以设置代理,但是使用Java代码,我无法做到这一点。
curl 命令:
**curl -I -x IP:80 URL**
在Java文件中完成代码更改:
HttpClient client = new HttpClient();
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
EntityEnclosingMethod method = new PostMethod(url);
method.setRequestEntity(new StringRequestEntity(requestXML, "text/xml", "utf-8"));
通过上述代码在我的java文件中的更改,我得到了以下错误:
java.net.ConnectException: Connection refused (Connection refused)
这表明我无法将该URL与尝试用于连接URL的代理连接。
最佳答案
我认为这可能会有所帮助:
HttpClient client = new HttpClient();
HostConfiguration config = client.getHostConfiguration();
config.setProxy("someProxyURL", "someProxyPort");
Credentials credentials = new UsernamePasswordCredentials("username", "password");
AuthScope authScope = new AuthScope("someProxyURL", "someProxyPort");
client.getState().setProxyCredentials(authScope, credentials);
EntityEnclosingMethod method = new PostMethod(url);
method.setRequestEntity(new StringRequestEntity(requestXML, "text/xml", "utf-8"));