4

In Apache Commons 4.3 and onwards, you need to inject your HTTP proxy settings at the HttpRequest level - but not globally, in the HttpClient itself:

RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet httpget = new HttpGet("http://url");
httpget.setConfig(config);
...

httpclient.execute(target, httpget);

The problem is that, in some higher-level libraries, configuration is made by passing a custom-built HttpClient instance. But that doesn't give you access to the HttpRequest built inside the API code.

For instance, in Jolokia (a JMX REST bridge), you create your client like this:

J4pClient j4pClient = new J4pClient("http://localhost:8080/jolokia", httpClient);

Is there a way to specify an HTTP proxy in this case ? The http.proxyHost system property seems ignored and i'd like to avoid creating a full layer of wrapper classes around HttpClient and HttpRequest to inject the settings during the request creation.

1 Answer 1

9

You can use HttpClientBuilder. It will pull in the system properties for http.proxyHost, http.proxyPort, http.nonProxyHosts.

HttpClientBuilder.create().useSystemProperties().build();

See http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html

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.