开发者

Proxy authentication problem with HttpClient

开发者 https://www.devze.com 2023-02-27 05:25 出处:网络
I have a thick client application which connects to a server on the internet for file transmission and upload.

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 no开发者_运维问答t 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?


For proxy and port, I would suggest using System property on the java command line like so:

java -Dhttp.proxyHost=myproxyserver.com -Dhttp.proxyPort=80 MyMainClass

User and password would be asked from the user through the GUI then set with the System.getProperties().put(String, String) method. Parameters are:

  • http.proxyUser
  • http.proxyPassword

The full documentation is available here. An article pertaining to your exact problem can be found here.

0

精彩评论

暂无评论...
验证码 换一张
取 消