[英]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.