Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the proxy on the command line when using org.apache.commons.httpclient?

If an application uses the java.net.* routines, I can set a proxy when invoking the application like this:

java -Dhttp.proxyHost=proxy.server.com -Dhttp.proxyPort=8000 <whatever-the-app-is>

However, I have an application (which I can't change) using org.apache.commons.httpclient to do the http communication. It doesn't specify a procxy server, but it does use the default HttpConnection. Is there some way I can tell the apache http client from the command line to use a proxy server?

like image 932
The Archetypal Paul Avatar asked Feb 03 '23 00:02

The Archetypal Paul


2 Answers

When using the HTTPClient builder use the useSystemProperties() method to enable the standard JVM -D proxy parameters.
See http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#useSystemProperties()

Example:

CloseableHttpClient httpclient = HttpClients.custom()
    .useSystemProperties()
    .build();

Now use -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 to configure the proxy.

like image 68
userM1433372 Avatar answered Feb 05 '23 15:02

userM1433372


Unfortunately, I don't think you can. The only way is for the application to read the System property and then set it in the DefaultHttpParams object.

Take a look at this thread on the httpclient-user group for more details.

like image 32
dogbane Avatar answered Feb 05 '23 16:02

dogbane