繁体   English   中英

系统代理设置,Java

[英]System proxy setting, Java

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

我的意思是像chrome这样的东西,它将打开系统的代理设置,然后使用它们。 如何做到并获取这些值? 还有其他理想方法吗?

其次,目前我正在检查是否有代理集。 如果是,我正在使用url.openConnection(proxy); 如果不是普通的url.openConnection(); 有更清洁的方法吗? 系统自动与代理集连接的位置。

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

从源代码中,我们可以使用

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

文献

也看一看: 如何设置JVM使用的代理

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

我遇到了同样的问题,想使用SOAP Client调用1个WSDL。 我能够通过SOAP UI调用WSDL,但是当我尝试通过JAVA代码包装请求时,它失败了。 我发现了问题,并且我的Java代码未获取代理服务器集。 我通过在Eclipse中设置以下代理来进行显式尝试:Eclipse-> Windows-> Preferences-> Geneal-> Network Connection。 将本机更改为手动,并添加了代理和端口。 尽管如此,它还是没有用。 最后,我在代码中仅添加了1行,并且所有代码都正常工作:System.setProperty(“ java.net.useSystemProxies”,“ true”); 只要正确设置了JAVA主目录,这肯定会在Eclipse中使用系统集代理。

感谢Saurabh M. Chande

您只需要: 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 ,则会遇到麻烦)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM