WebView SSL 错误处理
修复帮助
https://support.google.com/faqs/answer/7071387
原因
onReceivedSslError 中没有做 ssl 错误校验便直接 调用方法handler?.proceed()
解决方案
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
val url = view?.url ?: ""
if (url.isEmpty()) {
handler?.cancel()
return
}
val host = Uri.parse(url).host ?: ""
if (host.contains("test.com")) {
handler?.proceed()
return
}
var builder = AlertDialog.Builder(view?.context)
var message = when (error?.primaryError) {
SslError.SSL_UNTRUSTED -> {
}
SslError.SSL_EXPIRED -> {
}
SslError.SSL_IDMISMATCH -> {
}
SslError.SSL_NOTYETVALID -> {
}
SslError.SSL_INVALID -> {
}
SslError.SSL_DATE_INVALID -> {
}
else -> {
error?.primaryError.toString()
}
}
message += AppContext.getString(R.string.message_ssl_error);
builder.setTitle(R.string.title_ssl_error);
builder.setMessage(message);
builder.setPositiveButton(R.string.continued) { dialog: DialogInterface?, which: Int ->
handler?.proceed()
}
builder.setNegativeButton(R.string.cancel) { dialog: DialogInterface?, which: Int ->
handler?.cancel()
}
var dialog = builder.create()
dialog.show();
}
}