簡體   English   中英

使用Android WebView處理HTTP基本身份驗證

[英]Handling HTTP basic authentication using Android webview

我正在使用Web視圖加載需要基本HTTP身份驗證的網頁。 我已經做了一些Google搜索,並且知道可以使用WebClientView並覆蓋onReceivedHttpAuthRequest

像這樣

webView.setWebViewClient(new WebViewClient() {

        public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm)        {
            // TODO Auto-generated method stub

      handler.proceed(username, password); //question is here,let user input these. 
        }
    });

我已經嘗試過的是,我使用“登錄”活動獲取用戶的輸入並將參數傳遞回此Webview活動。

但這是最好的方法嗎?

使用HttPget和HttpPost嘗試此方法

        CredentialsProvider credProvider = new BasicCredentialsProvider();

        credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, 

        AuthScope.ANY_PORT,AuthScope.ANY_REALM),  new UsernamePasswordCredentials("admin", 
       "guest"));

        httpclient.setCredentialsProvider(credProvider);
        Log.w("SENCIDE", "Execute HTTP Get Request");
        HttpResponse response = httpclient.execute(httpget);

這是Httpget中的身份驗證代碼的示例代碼。 您可以按此嘗試嘗試...

暫無
暫無

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

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