Android WebView onReceivedHttpAuthRequest () not called again

I am using a webview based application where I am browsing the url in webview. Url has HTTP authentication.

When I run the URL for the first time, its onReceivedHttpAuthRequest () is called, and I show a dialog box for the user to enter authentication credentials, which are the username and password of the authentication.

@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 The request is sent correctly and the URL is loaded. But after I exit the application using the back button (I do not send it in the background), if I run it again and return it to load the same url, it directly loads the URL without asking for credentials that onReceivedHttpAuthRequest () is no longer called.

, :

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);

-, cookie webview:

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

, . -, . , , onReceivedHttpAuthRequest() ?

+4
2
  • url, http

    Map extraHeaders = new HashMap < > (); extraHeaders.put( "", "TlRM" );// .
    webView.loadUrl(url, extraHeaders);

  • WebViewClient

    @Override public void onReceivedHttpAuthRequest ( WebView, HttpAuthHandler,
                         String host, String realm) {   view.loadUrl(url);// URL-.   handler.proceed( , ); }

  • , -. , .

+1

- cookie. cookie URL-. .

HttpURLConnection connection = null;
    try {
        URL urls = new URL(url);
        String authStr = "";
        if (!params.isEmpty()) {
            authStr = params.get(0).getValue() + ":"
                    + params.get(1).getValue();
        }

        String authEncoded = Base64.encodeBytes(authStr.getBytes());

        connection = (HttpURLConnection) urls
                .openConnection();
        connection.setConnectTimeout(5100);
        connection.setReadTimeout(5200);
        connection.setDoOutput(false);
        connection.setRequestProperty("Authorization", "Basic "
                + authEncoded);
        CookieManager cookieManager = CookieManager.getInstance();
        String cookie = cookieManager.getCookie(connection.getURL().toString());
        if (cookie != null) {
            connection.setRequestProperty("Cookie", cookie);
        }
        connection.connect();

        responseCode = connection.getResponseCode();
        InputStream content = connection.getInputStream();
        response = convertStreamToString(content);
        content.close();

        // Get cookies from responses and save into the cookie manager. session is created only in rest call url
        List<String> cookieList = connection.getHeaderFields().get("Set-Cookie");
        if (cookieList != null) {
            for (String cookieTemp : cookieList) {
                cookieManager.setCookie(connection.getURL().toString(), cookieTemp);
            }
        }

    }catch (SocketTimeoutException es){
        response = ConnectionStatus.TIME_OUT.name();
    } catch (Exception e) {
        response = e.getMessage();
        e.printStackTrace();
    }
    finally {
        connection.disconnect();
    }
0

All Articles