系统代理设置,Java

时间:2012-10-31 07:04:40

标签: java url proxy

我有一个需要连接到网络的应用程序。在处理代理连接时我需要一些建议。目前,用户设置代理设置,因此我使用输入的信息进行连接。有没有更好的方法来处理这种情况。

我的意思是像chrome这样打开系统的代理设置,然后使用它们。怎么做并检索这些值?还有其他理想的方法吗?

其次,目前我正在检查是否有代理集。如果是,我正在使用url.openConnection(proxy); 如果不是那么简单url.openConnection();是否有更清洁的方式呢?系统自动与代理集连接的地方。

5 个答案:

答案 0 :(得分:3)

//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" );

答案 1 :(得分:2)

从源代码我们可以使用

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

命令行:

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

Document

答案 2 :(得分:0)

看看这个: How do I set the proxy to be used by the JVM

可以通过使用一些标志启动JVM来完成: JAVA_FLAGS = -Dhttp.proxyHost = 10.0.0.100 -Dhttp.proxyPort = 8800 java $ {JAVA_FLAGS}

答案 3 :(得分:0)

我遇到了同样的问题,想要使用SOAP Client调用1个WSDL。 我能够通过SOAP UI调用WSDL但是当我尝试通过我的JAVA代码包装请求时,它失败了。 我发现了问题,我的java代码没有拿起代理的设置。 我通过在Eclipse中设置这些代理来明确地尝试: Eclipse - > Windows - >偏好 - > Geneal - >网络连接。 将Native更改为Manual并添加了proxy&港口。 不过,它没有用。 最后,我在我的代码中只添加了一行,它全部工作: System.setProperty(“java.net.useSystemProxies”,“true”); 如果您的JAVA主页设置正确,这肯定会在Eclipse中获取系统集代理。

由于 Saurabh M. Chande

答案 4 :(得分:0)

您需要的所有内容:https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html

建议:请勿使用System.getProperties().put,请参阅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.
 * ....
 */

(如果您将Properties::put与非String值一起使用,则会遇到麻烦)

相关问题