2012-10-31 103 views
2

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

我的意思是像铬这打开系统的代理设置,然后使用它们。如何做到这一点,并检索这些值?任何其他理想的方法?

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

回答

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

+0

不使用“放”,看到https://stackoverflow.com/questions/ 13152861 /系统代理设置的Java/35696549#35696549 –

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

我面临同样的问题,并希望使用SOAP客户端调用1个WSDL。 我能够通过SOAP调用UI的WSDL但当我结束了在我的JAVA代码的要求,这是失败的。 我发现这个问题,我的java代码没有拿起代理的设置。 - >窗口 - >首选项 - >驻港公署 - >网络连接 的Eclipse: 我通过我的Eclipse中设置这些代理明确地尝试。 将原生更改为手动并添加了代理&端口。 不过,它没有工作。 最后,我只加1行我的代码中,它的工作都: System.setProperty(“java.net.useSystemProxies”,“真”); 如果您的JAVA主页设置正确,这肯定会在Eclipse中选择系统集代理。

感谢 SAURABH M.缠得

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与非字符串值)