gpt4 book ai didi

android - 我如何修复 ssl 异常错误并在 android webview 中加载 https

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:25 58 4
gpt4 key购买 nike

我正在尝试在 webview 中加载 https 端,但它会给我 ssl 证书错误。我的网站已经有 ssl 证书,所以我不明白为什么它不允许加载安全 url。我遵循一些代码提供下面的代码,但我的客户要求他不想在加载网站之前出现对话框。那么我如何使用 ssl 证书加载我的网站。任何想法?我不想使用下面的解决方案。

       @Override
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
// final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// builder.setMessage(R.string.notification_error_ssl_cert_invalid);
// builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// handler.proceed();
// }
// });
// builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// handler.cancel();
// finish();
// }
// });
// final AlertDialog dialog = builder.create();
// dialog.show();





String msg="";
if(error.getPrimaryError()==SslError.SSL_DATE_INVALID
|| error.getPrimaryError()== SslError.SSL_EXPIRED
|| error.getPrimaryError()== SslError.SSL_IDMISMATCH
|| error.getPrimaryError()== SslError.SSL_INVALID
|| error.getPrimaryError()== SslError.SSL_NOTYETVALID
|| error.getPrimaryError()==SslError.SSL_UNTRUSTED) {
if(error.getPrimaryError()==SslError.SSL_DATE_INVALID){
msg="The date of the certificate is invalid";
}else if(error.getPrimaryError()==SslError.SSL_INVALID){
msg="A generic error occurred";
}
else if(error.getPrimaryError()== SslError.SSL_EXPIRED){
msg="The certificate has expired";
}else if(error.getPrimaryError()== SslError.SSL_IDMISMATCH){
msg="Hostname mismatch";
}
else if(error.getPrimaryError()== SslError.SSL_NOTYETVALID){
msg="The certificate is not yet valid";
}
else if(error.getPrimaryError()==SslError.SSL_UNTRUSTED){
msg="The certificate authority is not trusted";
}
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(msg);
builder.setPositiveButton("continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.proceed();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();




}

最佳答案

如果您需要 HTTPS 连接的证书,您可以将 .bks 文件作为原始资源添加到您的应用程序并扩展 DefaultHttpConnection,以便您的证书用于 HTTPS 连接。

public class MyHttpClient extends DefaultHttpClient {

private Resources resources;

public MyHttpClient(Resources resources) {
this.resources = resources;
}

@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
if (resources != null) {
registry.register(new Scheme("https", newSslSocketFactory(), 443));
} else {
registry.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
}
return new SingleClientConnManager(getParams(), registry);
}

private SSLSocketFactory newSslSocketFactory() {
try {
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = resources.openRawResource(R.raw.mystore);
try {
trusted.load(in, "pwd".toCharArray());
} finally {
in.close();
}
return new SSLSocketFactory(trusted);
} catch (Exception e) {
throw new AssertionError(e);
}
}
}

这可能会解决您的 HTTPS ssl 证书错误。

替代方案

使用下面的代码

webView.setWebViewClient(new SSLTolerentWebViewClient());
webView.loadUrl(myhttps url);

和 SSLTolerentWebViewClient 类

private class SSLTolerentWebViewClient extends WebViewClient {
public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {

AlertDialog.Builder builder = new AlertDialog.Builder(Tab1Activity.this);
AlertDialog alertDialog = builder.create();
String message = "SSL Certificate error.";
switch (error.getPrimaryError()) {
case SslError.SSL_UNTRUSTED:
message = "The certificate authority is not trusted.";
break;
case SslError.SSL_EXPIRED:
message = "The certificate has expired.";
break;
case SslError.SSL_IDMISMATCH:
message = "The certificate Hostname mismatch.";
break;
case SslError.SSL_NOTYETVALID:
message = "The certificate is not yet valid.";
break;
}

message += " Do you want to continue anyway?";
alertDialog.setTitle("SSL Certificate Error");
alertDialog.setMessage(message);
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Ignore SSL certificate errors
handler.proceed();
}
});

alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

handler.cancel();
}
});
alertDialog.show();
}
}

关于android - 我如何修复 ssl 异常错误并在 android webview 中加载 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43883750/

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