Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-WebView's onReceivedHttpAuthRequest() not called again

I am using a webview based application where i am rendering a url in the webview. The Url has a HTTP auth .

When i launch the url very first time,its onReceivedHttpAuthRequest() is called and I display a dialog for user to enter the authentication credentials that is auth username and password.

@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        final WebView mView = view;
        final HttpAuthHandler mHandler = handler;

        final EditText usernameInput = new EditText(mActivity);
        usernameInput.setHint("Username");

        final EditText passwordInput = new EditText(mActivity);
        passwordInput.setHint("Password");
        passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        LinearLayout ll = new LinearLayout(mActivity);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.addView(usernameInput);
        ll.addView(passwordInput);

        Builder authDialog = new AlertDialog
                .Builder(mActivity)
                .setTitle("Authentication")
                .setView(ll)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        mHandler.proceed(usernameInput.getText().toString(), passwordInput.getText().toString());
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                        mView.stopLoading();
                        onLoadListener.onAuthCancel((MyWebView)mView, mTitleTextView);
                    }
                });

        if(view!=null)
            authDialog.show();

    }

On Submitting the request proceed well and the url is loaded. But After I exit the app using back button(not sending in background), if i launch it again and tru to load the same url it directly load the url without asking for credentials that is onReceivedHttpAuthRequest() is never called again.

I am also clearing the credentials on app exit using following code:

WebViewDatabase webDB = WebViewDatabase.getInstance(BrowserActivity.this);
    if(webDB!=null){
        if(webDB.hasFormData())
            webDB.clearFormData();
        if(webDB.hasUsernamePassword())
            webDB.clearUsernamePassword();
        if(webDB.hasHttpAuthUsernamePassword())
            webDB.clearHttpAuthUsernamePassword();
    }
webView.clearCache(true);

Also i am clearing all the webview cache, cookies, and application's cache directory and the webview databases:

BrowserActivity.this.deleteDatabase("webview.db");
BrowserActivity.this.deleteDatabase("webviewCache.db");

I don't know why this is happening. Is there anybody who can help me on this. At least on the issue that why onReceivedHttpAuthRequest() is not called?

like image 988
mohitum Avatar asked Dec 05 '13 11:12

mohitum


People also ask

What is shouldOverrideUrlLoading android?

shouldOverrideUrlLoading(WebView view, WebResourceRequest request) Give the host application a chance to take control when a URL is about to be loaded in the current WebView. boolean. shouldOverrideUrlLoading(WebView view, String url) This method was deprecated in API level 24.

What is WebViewClient Android?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.

What is a WebViewClient?

WebViewClient is the object responsible for most the actions inside a WebView. JavaScript enabled, security, routing, etc. You can make a custom one, as well as use a Chrome one.


1 Answers

  1. before load the url,set Authorization information in http headers

    Map extraHeaders = new HashMap<>(); extraHeaders.put("Authorization", "TlRM");//the value is Casually set.
    webView.loadUrl(url, extraHeaders);

  2. in WebViewClient

    @Override public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler,
    String host, String realm) { view.loadUrl(url);//reload the url.
    handler.proceed(account, password); }

  3. I resolve this issue,but I can not speak English very well. Hope it can help you.

like image 100
hankin Avatar answered Oct 19 '22 01:10

hankin