gpt4 book ai didi

Android-WebView 的 onReceivedHttpAuthRequest() 没有被再次调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:02 25 4
gpt4 key购买 nike

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

当我第一次启动 url 时,它的 onReceivedHttpAuthRequest() 被调用,我显示一个对话框供用户输入身份验证凭据,即 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 已加载。但是在我使用后退按钮退出应用程序(不在后台发送)后,如果我再次启动它并真正加载相同的 url,它会直接加载 url 而无需再次调用 onReceivedHttpAuthRequest() 的凭据。

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

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 缓存、cookie、应用程序的缓存目录和 webview 数据库:

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

我不知道为什么会这样。有没有人可以帮助我。至少在为什么不调用 onReceivedHttpAuthRequest() 的问题上?

最佳答案

  1. 在加载url之前,在http头中设置授权信息

    Map extraHeaders = new HashMap<>();extraHeaders.put("Authorization", "TlRM");//随便设置的值。
    webView.loadUrl(url, extraHeaders);

  2. 在 WebViewClient 中

    @覆盖public void onReceivedHttpAuthRequest(WebView View ,HttpAuthHandler 处理程序,
    字符串主机,字符串领域){ view.loadUrl(url);//重新加载url。
    handler.proceed(账号,密码);

  3. 我解决了这个问题,但我的英语说得不是很好。希望对您有所帮助。

关于Android-WebView 的 onReceivedHttpAuthRequest() 没有被再次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com