2

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?

1
  • 1
    Which version do you use? This HostConfiguration looks like 3.1 (i.e. commons HttpClient), and I didn't find anything about authentication here. For HTTP Components (e.g. version 4.*), there is an example on the Example page. Commented Apr 19, 2011 at 21:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.