如何在Java中的HttpClient请求上设置代理主机 [英] How to set proxy host on HttpClient request in Java

查看:726
本文介绍了如何在Java中的HttpClient请求上设置代理主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在通过URL发送HttpClient请求之前设置代理.因为我能够连接它,所以curl命令可以设置代理,但是使用Java代码,我无法做到这一点.

I want to set proxy before sending HttpClient request on a URL. As I am able to connect it curl command setting up the proxy but with Java code I am not able to do that.

卷曲命令:

**curl -I -x IP:80  URL**

在Java文件中完成代码更改:

Code change done in java file:

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文件中更改了上面的代码后,我得到了以下错误:

With above code changes in my java file I am getting below error :

java.net.ConnectException: Connection refused (Connection refused)

这表明我无法将该URL与尝试用于连接该URL的代理服务器连接.

Which shows that I am not able to connect that URL with the proxy I am trying to use to connect the URL.

推荐答案

我认为这可能会有所帮助:

I think this could be helpful:

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"));

这篇关于如何在Java中的HttpClient请求上设置代理主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆