2016-02-01 33 views
0

我已經搜索了很多這個主題,但不明白爲什麼如果我嘗試在我的Java應用程序中使用Web服務,我會失敗,而不執行此代碼段以及:Java默認身份驗證與代理參數

final String authUser = "myUser"; 
    final String authPassword ="******"; 
    Authenticator.setDefault(
     new Authenticator() { 
      public PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(
        authUser, authPassword.toCharArray()); 
      } 
     } 
    ); 

...我不喜歡在這裏「雕刻」我的憑據,或者在屬性文件中。 我寧願使用「運行方式」 VM參數......所以,如果我評論上面的代碼並運行我的應用程序:

-Dhttps.proxyHost=proxy.test.it 
-Dhttp.proxyPort=3128 
-Dhttp.proxyUsername=myUser 
-Dhttps.proxyPassword=**** 
-Danypoint.platform.proxy_username=myUser 
-Danypoint.platform.proxy_password=**** 
-Danypoint.platform.proxy_host=proxy.test.it 
-Danypoint.platform.proxy_port=3128 
-Djava.net.useSystemProxies=true 
-Dhttp.nonProxyHosts=... 

或...

System.setProperty("http.proxyUser", authUser); 
    System.setProperty("http.proxyPassword", authPassword); 
    System.setProperty("http.nonProxyHosts","myHost"); 
    System.setProperty("useSystemProxies","true"); 

它贏得沒有工作。我真的不明白。

感謝支持!

+0

你總是可以做'字符串AUTHUSER = System.getProperty(「http.proxyUser」)'所以它的價值不是在硬編碼的代碼,但肯定CMD線 – ZhongYu

+0

讀,但它不工作!請讀我的句子。如果和只有如果我把firt java腳本,電話將不會失敗... –

回答

0

我不知道你想實現什麼,但如果你在你的數據通過VM arugments通過這樣

-Dhttp.proxyUsername=myUser 
-Dhttps.proxyPassword=**** 

你會像這樣訪問的數據。

String authUser = System.getProperty("http.proxyUser"); 
+0

請閱讀更好的我的線程。這種方式是行不通的。 –

+0

這種方式*做*工作。你沒有正確使用它。不要聲明'''authUser'''兩次。 (在你的例子中你已經標記爲'''final ......'''如果你這樣做了,你的應用程序就會按照你的想法工作 – DominicEU

+0

如果我想完全避免那件事如果沒有這個java代碼片段,就不可能讓它工作?正如我所說的,我已經評論過這個部分...... –