2012-10-31 101 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與非字符串值)