40

I found these system settings http.proxyHost and http.proxyPort are of no use to httpClient. How to force the httpClient to use proxy by environment variables or VM arguments or something like those without changing code?

Sven Viking
  • 2,660
  • 21
  • 34
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 1
    Have you found a workaround for this question? I am facing the same issue, a third party library (azure-keyvault-java-sdk) uses its own httpClient and I am unable to force it to use a proxy. The actual http calls are made in new Executor threads which does not allow me to set the ProxySelector in the calling thread as well. Any solutions? – ameenhere Jan 11 '17 at 08:58
  • We have the same issue, we want to do this without a code change where even a configuration change is considered a code change since we need to issue a new deployment to do so. Our conclusion is that it is not possible to override the client's configuration with JVM command line options, you just have to make a change to the client's configuration and issue a new release. – Cheefachi Dec 07 '19 at 18:57

5 Answers5

22

in https://issues.apache.org/jira/browse/HTTPCLIENT-1128

SystemDefaultHttpClient was added to ver. 4.2

see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/SystemDefaultHttpClient.html

dove
  • 20,469
  • 14
  • 82
  • 108
Krystian Nowak
  • 221
  • 2
  • 3
  • 6
    As @wangf noted below, you can also use `.useSystemProperties()` before calling `.build()` on any HttpClientBuilder to read system properties such as `http.proxyHost` and `http.proxyPort`: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#useSystemProperties() – bodecker Mar 14 '17 at 23:20
22

HTTP client (v 4.5.1 for my case) can use system proxy like this:

HttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();
//or 
HttpClient httpClient = HttpClients.createSystem();
wangf
  • 895
  • 9
  • 12
  • Exactly, you can use the system properties like this. Do not forget that for connection to https://... urls you have to use https.proxyHost etc properties. A similar issue with details answer from my side: http://stackoverflow.com/questions/30630330/what-java-properties-to-pass-to-a-java-app-to-authenticate-with-a-http-proxy/ – Andreas Panagiotidis Oct 06 '16 at 06:46
6

you can force proxy to HttpClient by yourself with client.getHostConfiguration().setProxy(host, port) method. I usually create wrapper class around HttpClient and when initializing this class I setup proxy from whatever source (env. variables ...).

I used java.net.ProxySelector.setDefault(new MyProxySelector()) in situation where you can't set proxy directly on HttpClient. You have to implement your own ProxySelector class and method select makes proxy selection based on requested URI. You can make url->proxy mapping to configure particular URI address to required proxy or return one proxy for all requested URI globally.

As I can see in HttpClient source code, there's no other way how to configure proxy only setProxy method. I'm using commons-httpclient-3.1.

michal.kreuzman
  • 12,170
  • 10
  • 58
  • 70
  • @user496949 Can you create wrapper class around `HttpClient` and initialize it always by your way? – michal.kreuzman Mar 02 '11 at 09:32
  • 1
    say, I have a 3rd party library, it uses httpclient inside to get something. How to dela with this situation? – user496949 Mar 02 '11 at 09:34
  • 1
    In this case I used `java.net.ProxySelector.setDefault(new MyProxySelector());`. You have to implement your own ProxySelector class and method select makes proxy selection based on requested URI (Make url<->proxy mapping to configure particular URL address to required proxy ). As I can see in `HttpClient` source code, there's no other way how to configure proxy even setProxy method. I'm using commons-httpclient-3.1. – michal.kreuzman Mar 02 '11 at 09:48
0

AFAIK, you can't manage this without code changes but you can get closer to native behaviour by using your own connection manager. See ProxySelector changes URL's scheme from https:// to socket://

Community
  • 1
  • 1
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
-16

Does this help?

System.setProperty("https.proxyHost", proxy_host);
System.setProperty("http.proxyHost", proxy_host);
System.setProperty("https.proxyPort", proxy_port);
System.setProperty("http.proxyPort", proxy_port);

Or ofcourse you can pass the same properties via the commandline

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94