2011-02-01 87 views
3

我在做什麼:「java.lang.UnsupportedOperationException:尚不支持。」

我想連接到Web門戶[使用https]使用Java。 供應使用驗證器class.When我運行該程序的用戶憑據我寫的代碼,我得到一個異常:「java.lang.UnsupportedOperationException:尚不支持」

我的代碼貼:

 

public class Main { 

    public class MyAuthenticator extends Authenticator { 

     protected PasswordAuthentication getpasswordAuthentication() { 
      String username = "username"; 
      String password = "password"; 
      // Return the information 
      return new PasswordAuthentication(username, password.toCharArray()); 
     } 

     @Override 
     public Result authenticate(HttpExchange he) { 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
    } 

    public static void main(String[] args) { 
     // TODO code application logic here 


     TrustManager[] trustClients = new TrustManager[]{ 
      new X509TrustManager() { 

       public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { 
        throw new UnsupportedOperationException("Not supported yet."); 
       } 

       public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { 

        throw new UnsupportedOperationException("Not supported yet."); 
       } 

       public X509Certificate[] getAcceptedIssuers() { 
        return null; 
        //throw new UnsupportedOperationException("Not supported yet."); 
       } 
      } 
     }; 
     try { 
      SSLContext sslcontext = SSLContext.getInstance("SSL"); 
      sslcontext.init(null, trustClients, new java.security.SecureRandom()); 
      HttpsURLConnection.setDefaultSSLSocketFactory(sslcontext.getSocketFactory()); 

     } catch (Exception e) { 
      System.out.println("in 1st catch " + e); 
     } 


     try { 
      URL url = new URL("https://someURL.server.com/fmk/jsp/ltpaLogin.jsp"); 
      URLConnection urlconnection = url.openConnection(); 
      System.out.println(urlconnection); 
      System.out.print(urlconnection.getInputStream().toString()); 

     } catch (Exception e) { 
      System.out.println("in 2ndcatch " + e.getMessage()); 
     } 
    } 
} 

 

異常「java.lang.UnsupportedOperationException:不支持」在第二次嘗試中引發。我的方法是否正確?如果沒有,有沒有其他辦法可以這樣做?

當我打開網絡瀏覽器,我得到了登錄頁面的頁面;一旦我提供我的憑據的門戶網站顯示

任何一個可以請建議如何打webportal並提供我的憑據,並檢查認證成功與否? 任何示例代碼片段將真正有幫助 在此先感謝..

回答

6

你見過這段代碼片段嗎?

@Override 
    public Result authenticate(HttpExchange he) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

恰恰是每次嘗試進行身份驗證時都會拋出上述異常的人。

因此,在我看來,解決您的身份驗證問題非常簡單:實現此方法。

+0

OOppss !!這是由Netbeans產生的!我注意到這一點!我會實現這一點,並嘗試非常感謝。 – ManiVI 2011-02-03 16:25:40

7

,如果你不希望實現此方法:

@Override 
    public Result authenticate(HttpExchange he) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

處之泰然,只刪除這一行的方法:

throw new UnsupportedOperationException("Not supported yet."); 
+0

是的,我會嘗試一下,非常感謝! – ManiVI 2011-02-03 16:26:54

+1

刪除行會導致編譯時錯誤。你至少需要添加一個`return null;`。 – 2013-10-19 21:43:53

2

你可能使用的IDE如NetBeans中實現一個接口/覆蓋需要某些方法實現的抽象類。當IDE做到這一點時(Netbeans肯定會這樣做),他們爲你生成方法存根,所以代碼仍然可以編譯,但是如果你嘗試調用一個你沒有實現的方法,你會得到一個不可避免的錯誤。

通常這意味着你必須實現給定的方法,但有時一個實現確實需要一個空白方法存根,在這種情況下只需刪除引發異常的那一行。

0

當您沒有爲您實現的接口提供所有方法的實現時,就會發生這種情況。檢查你沒有執行的方法。

相關問題