HttpClient 的代理身份验证问题
我有一个胖客户端应用程序,它连接到互联网上的服务器进行文件传输和上传。
然而,访问互联网是通过代理。我在胖客户端上使用 HttpClient,在服务器端使用 Apache Commons 文件上传。
我在 HttpClient 上设置代理,如下所示:
HttpClient client = new HttpClient();
HostConfiguration config = client.getHostConfiguration();
config.setProxy(PROXY_HOST, PROXY_PORT);
PostMethod filePost = new PostMethod(servletPath);
int status = client.executeMethod(config , filePost);
但是,我使用 HttpUrlConnection 来获取代理信息并设置为这些值,而不是对上面的 PROXY_HOST、PROXY_PORT 进行硬编码值。这很好用。
如果我不这样设置代理设置,HttpClient 将忽略我的代理设置并且不会自动检测它们,因此我的应用程序无法连接到互联网上的服务器。
现在,当我使用 HttpClient 连接到服务器时,请求通过代理进行,但失败,因为它需要所提供的代理信息的用户身份验证凭据。我无法找到一种方法来完成这项工作,因为我期望用户在连接到代理后会弹出一个输入用户 ID 和密码的窗口,而不是请求完全失败。
有人可以建议如何使 HttpClient 与代理一起使用,而无需对 PROXY_HOST、PROXY_PORT 值进行硬编码。
此外,该应用程序将从胖客户端为不同的用户启动。因此,应该从浏览器设置中自动检测代理信息(这就是 HttpUrlConnection 为我做的事情)。
有人可以为这种情况建议一个解决方案吗?
I have a thick client application which connects to a server on the internet for file transmission and upload.
However, the access to the internet is via proxy. I am using HttpClient on the thick client and Apache Commons file upload on the server side.
I am setting the proxy on the HttpClient as below:
HttpClient client = new HttpClient();
HostConfiguration config = client.getHostConfiguration();
config.setProxy(PROXY_HOST, PROXY_PORT);
PostMethod filePost = new PostMethod(servletPath);
int status = client.executeMethod(config , filePost);
However, instead of hardcoding values for PROXY_HOST, PROXY_PORT above, I am using HttpUrlConnection to get the proxy information and set into these values. This works fine.
If I dont set the proxy settings like this, HttpClient is ignoring my proxy settings and not detecting them automatically and as a result my application is not able to connect to the server on the internet.
Now when I connect to the server using HttpClient, the request goes via proxy but fails as it expects user authentication credentials for the proxy information provided. I am not able to figure out a way how to make this work as I expected a popup for the user to enter user id and password, once connected to the proxy instead of request failing altogether.
Can somebody suggest how to make HttpClient work with proxy without hardcoding the PROXY_HOST, PROXY_PORT values.
Also, this application will be launched from thick client for different users. So the proxy information should be automatically detected from browser settings (which is what the HttpUrlConnection is doing for me).
Can somebody please suggest a solution for this scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于代理和端口,我建议在 java 命令行上使用系统属性,如下所示:
通过 GUI 向用户询问用户和密码,然后使用 System.getProperties().put(String, String) 方法进行设置。参数为:
完整文档可用 这里。您可以在此处。
For proxy and port, I would suggest using System property on the java command line like so:
User and password would be asked from the user through the GUI then set with the System.getProperties().put(String, String) method. Parameters are:
The full documentation is available here. An article pertaining to your exact problem can be found here.