How do I set the proxy to be used by the JVM

前端 未结 19 1925
孤街浪徒
孤街浪徒 2020-11-22 05:51

Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.

I am behind

19条回答
  •  孤城傲影
    2020-11-22 06:17

    The following shows how to set in Java a proxy with proxy user and proxy password from the command line, which is a very common case. You should not save passwords and hosts in the code, as a rule in the first place.

    Passing the system properties in command line with -D and setting them in the code with System.setProperty("name", "value") is equivalent.

    But note this

    Example that works:

    C:\temp>java -Dhttps.proxyHost=host -Dhttps.proxyPort=port -Dhttps.proxyUser=user -Dhttps.proxyPassword="password" -Djavax.net.ssl.trustStore=c:/cacerts -Djavax.net.ssl.trustStorePassword=changeit com.andreas.JavaNetHttpConnection
    

    But the following does not work:

    C:\temp>java com.andreas.JavaNetHttpConnection -Dhttps.proxyHost=host -Dhttps.proxyPort=port -Dhttps=proxyUser=user -Dhttps.proxyPassword="password" -Djavax.net.ssl.trustStore=c:/cacerts -Djavax.net.ssl.trustStorePassword=changeit
    

    The only difference is the position of the system properties! (before and after the class)

    If you have special characters in password, you are allowed to put it in quotes "@MyPass123%", like in the above example.

    If you access an HTTPS service, you have to use https.proxyHost, https.proxyPort etc.

    If you access an HTTP service, you have to use http.proxyHost, http.proxyPort etc.

提交回复
热议问题