System proxy setting, Java

2019-07-23 19:46发布

问题:

I have an application which needs to connect to net. I need some advice when dealing with proxy Connections. Currently the user sets the proxy settings and hence I use the entered information to connect. Is there a better way to deal with such situations.

I mean something like chrome which opens system's proxy settings and then uses them. How to do it and retrieve those values? Any other Ideal method?

Secondly, currently I am checking if there is a proxy set or not. If yes, I am using url.openConnection(proxy); IF not then plain url.openConnection(); Is there a more cleaner way of doing it? where system automatically connects with proxy set.

回答1:

From source code we can use

System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName");
System.getProperties().put("http.proxyPassword", "Password");

Command Line :

  $> java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=UserName -Dhttp.proxyPassword=Password ProxyClassHere

Document



回答2:

//Set the http proxy to webcache.mydomain.com:8080

System.setProperty( "http.proxyHost", "webcache.mydomain.com" );
System.setProperty( "http.proxyPort", "8080" );

System.setProperty( "https.proxyHost", "webcache.mydomain.com" );
System.setProperty( "https.proxyPort", "8080" );


回答3:

Take a look at this too: How do I set the proxy to be used by the JVM

It can be done by starting the JVM with some flags: JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 java ${JAVA_FLAGS}



回答4:

I was facing the same problem and wanted to call 1 WSDL using SOAP Client. I was able to call the WSDL through SOAP UI But when i tried wrapping up the request through my JAVA code, it was failing. I found the problem and my java code was not picking up the proxy's set. I tried explicitely by setting these proxies within my Eclipse : Eclipse -> Windows -> Preferences -> Geneal -> Network Connection. Changed Native to Manual and added proxy & Port. Still, it did not work. Finally, I added only 1 line within my code and it worked all : System.setProperty("java.net.useSystemProxies", "true"); This will surely pick up the system set proxy within Eclipse provided your JAVA home is set correctly.

Thanks Saurabh M. Chande



回答5:

All you need: https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html

Advice: Do not use System.getProperties().put, see http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Properties.java

/**
 * ....
 * Because {@code Properties} inherits from {@code Hashtable}, the
 * {@code put} and {@code putAll} methods can be applied to a
 * {@code Properties} object.  Their use is strongly discouraged as they
 * allow the caller to insert entries whose keys or values are not
 * {@code Strings}.  The {@code setProperty} method should be used
 * instead.
 * ....
 */

(you will get in trouble if you use Properties::put with a non-String value)



标签: java url proxy