I want to set proxy before sending HttpClient
request on a URL. As I am able to connect it curl command setting up the proxy but with Java code I am not able to
Add maven dependency (4.2.X+):
org.apache.httpcomponents
httpclient
4.5.3
commons-logging
commons-logging
Use HttpClientBuilder
and set flag useSystemProperties
:
HttpClient client = HttpClientBuilder.create().useSystemProperties().build();
then you have two options at least:
Option A: Override arguments, i.e.
java -Djava.net.useSystemProxies=true
-Dhttp.proxyHost=PROXY_HOST
-Dhttp.proxyPort=PROXY_PORT
-Dhttp.proxyUser=USERNAME
-Dhttp.proxyPassword=PASSWORD
-jar your-app.jar
Option B: Set up JVM (${JAVA_HOME}/lib/net.properties
):
java.net.useSystemProxies=true
http.proxyHost=some-host
http.proxyPort=some-port
and run your application