Android 安卓网络视图';s OnReceiveDhtPauthRequest()未再次调用

Android 安卓网络视图';s OnReceiveDhtPauthRequest()未再次调用,android,webview,android-webview,basic-authentication,webviewclient,Android,Webview,Android Webview,Basic Authentication,Webviewclient,我使用的是一个基于webview的应用程序,其中我在webview中呈现url。该Url具有HTTP身份验证 当我第一次启动url时,会调用它的onReceiveDhtPauthRequest(),并显示一个对话框,供用户输入身份验证凭据,即auth用户名和密码 @Override public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {

我使用的是一个基于webview的应用程序,其中我在webview中呈现url。该Url具有HTTP身份验证

当我第一次启动url时,会调用它的onReceiveDhtPauthRequest(),并显示一个对话框,供用户输入身份验证凭据,即auth用户名和密码

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

    }
在提交请求时,将顺利进行并加载url。但在我使用“后退”按钮退出应用程序(不在后台发送)后,如果我再次启动应用程序并tru加载相同的url,它将直接加载url,而不要求提供凭据,并且不会再次调用OnReceiveDhtPauthRequest()

我还在使用以下代码清除应用程序退出时的凭据:

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);
此外,我正在清除所有webview缓存、cookies和应用程序的缓存目录以及webview数据库:

BrowserActivity.this.deleteDatabase("webview.db");
BrowserActivity.this.deleteDatabase("webviewCache.db");
我不知道为什么会这样。有人能帮我吗。
至少在为什么不调用onReceiveDhtPauthRequest()的问题上?

原因是webview存储以前的连接cookies。因此,我们需要手动清除它们,并为当前url存储新的cookie数据。我们可以这样做

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();
    }
HttpURLConnection=null;
试一试{
URL URL=新URL(URL);
字符串authStr=“”;
如果(!params.isEmpty()){
authStr=params.get(0.getValue()+”:“
+参数get(1.getValue();
}
字符串authcoded=Base64.encodeBytes(authStr.getBytes());
连接=(HttpURLConnection)URL
.openConnection();
connection.setConnectTimeout(5100);
connection.setReadTimeout(5200);
连接设置输出(假);
connection.setRequestProperty(“授权”、“基本”
+作者编码);
CookieManager CookieManager=CookieManager.getInstance();
字符串cookie=cookieManager.getCookie(connection.getURL().toString());
if(cookie!=null){
setRequestProperty(“Cookie”,Cookie);
}
connection.connect();
responseCode=connection.getResponseCode();
InputStream内容=connection.getInputStream();
响应=convertStreamToString(内容);
content.close();
//从响应中获取cookie并保存到cookie管理器中。会话仅在rest调用url中创建
List cookieList=connection.getHeaderFields().get(“设置Cookie”);
如果(cookieList!=null){
for(字符串cookieTemp:cookieList){
cookieManager.setCookie(connection.getURL().toString(),cookieTemp);
}
}
}捕获(SocketTimeoutException es){
response=ConnectionStatus.TIME_OUT.name();
}捕获(例外e){
response=e.getMessage();
e、 printStackTrace();
}
最后{
连接断开();
}
  • 加载url之前,请在http头中设置授权信息

    Map extraHeaders=newhashmap(); 外部标题。put(“授权”、“TlRM”)//该值是随意设置的。
    loadUrl(url,extraHeader)

  • 在WebViewClient中

    @凌驾 公共无效OnReceiveDhtPauthRequest(WebView视图,HttpAuthHandler处理程序,
    字符串主机,字符串域){ view.loadUrl(url);//重新加载url。
    处理程序。继续(帐户、密码); }

  • 我解决了这个问题,但我的英语说得不是很好。 希望它能帮助你


  • 我使用“Authorization”标题而不是OnReceiveDhtPauthRequest()解决了此问题。

    您确定要清除保存的用户名/密码的代码正在执行吗?是的,它正在执行。另外,当我强制停止应用程序然后启动时,它会请求auth.OK,所以听起来只有当你使用“后退”按钮时才不起作用?您究竟什么时候运行清除数据库的代码?1。正在清除Webview缓存。2.删除数据库。3.清除饼干。4.删除缓存directory@Thushara,还没有,我现在已经改变了功能。但仍然在寻找同样的东西。