簡體   English   中英

“java.lang.UnsupportedOperationException:尚不支持。”

[英]“java.lang.UnsupportedOperationException: Not supported yet.”

我正在嘗試做的事情:

我正在嘗試使用 Java 連接到 [使用 https] 的 Web 門戶。 我已經編寫了使用 Authenticator 類提供用戶憑據的代碼。當我運行程序時,出現異常:

“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:尚不支持”。我的方法正確嗎? 如果沒有,還有其他選擇嗎?

當我在網絡瀏覽器中打開頁面時,我會看到登錄頁面;一旦我提供了我的憑據,就會顯示網絡門戶

任何人都可以建議如何訪問門戶網站並提供我的憑據並檢查身份驗證是否成功? 任何示例代碼片段都會非常有幫助 提前致謝..

你見過這個代碼片段嗎?

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

正是在您每次嘗試進行身份驗證時都會拋出上述異常的那個。

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

如果你不想實現這個方法:

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

放輕松,只刪除這行方法:

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

您可能使用過諸如 Netbeans 之類的 IDE 來實現接口/覆蓋需要實現某些方法的抽象類。 當 IDE 執行此操作時(Netbeans 肯定會這樣做),它們會像這樣為您生成方法存根,因此代碼仍然可以編譯,但是如果您嘗試調用尚未實際實現的方法,則會出現不可避免的錯誤。

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

當您沒有為已實現的接口中的所有方法提供實現時,就會發生這種情況。 檢查您未實施的方法。

你沒有給你的方法一個主體。 檢查出現問題的行並使用所需的代碼覆蓋您的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM