我正在使用基于webview的应用程序,我在webview中呈现网址。 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而不要求提供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()的问题上?
答案 0 :(得分:2)
在加载网址之前,请在http标头中设置授权信息
映射extraHeaders = new HashMap<>();
extraHeaders.put("授权"," TlRM"); //值是随便设置的。
webView.loadUrl(url,extraHeaders);
@覆盖
public void onReceivedHttpAuthRequest(WebView视图,HttpAuthHandler处理程序,
String host,String realm){
view.loadUrl(url); //重新加载网址
handler.proceed(account,password);
}
我解决了这个问题,但我不会说英语。 希望它可以帮到你。
答案 1 :(得分:0)
原因是webview存储以前的连接cookie。因此,我们需要手动清除它们并存储当前网址的新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();
}
答案 2 :(得分:0)
我使用“ Authorization”标头而不是onReceivedHttpAuthRequest()解决了它