Webview HTTPS 握手失败
Webview HTTPS handshake failed
我正在尝试从带有 WebView 库的 Android 应用程序访问网站,所以我有:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(
new SSLTolerentWebViewClient()
);
myWebView.loadUrl("https://www.mywebsite.ro");
}
}
class SSLTolerentWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if (error.toString() == "piglet")
handler.cancel();
else
handler.proceed(); // Ignore SSL certificate errors
}
}
在调试中我有:
D/OpenSSLLib: OpensslErr:Module:20(119:258); file:external/openssl/ssl/s23_clnt.c ;Line:714;Function:ssl23_get_server_hello
W/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:171 [0209/105028:WARNING:ssl_client_socket_openssl.cc(171)] Unmapped error reason: 258
E/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:792 [0209/105028:ERROR:ssl_client_socket_openssl.cc(792)] handshake failed; returned -1, SSL error code 1, net_error -2
D/chromium/tcp: LogConnectCompletion with error:-2
D/WebRequest: request error: -2
D/chromium: Unknown chromium error: -2
V/webview: setCertificate=null
V/webcore: setupViewport mRestoredScale=0.0 mViewScale=2.0 mTextWrapScale=2.0 data.mScale= 2.0
V/webcore: viewSizeChanged w=360; h=559; textwrapWidth=360; scale=2.0
D/skia: Flag is not 10
如果您没有将您的应用上传到 Play 商店,您可以忽略 SSL 证书错误:
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
这个问题是因为4.4以下的android版本不支持新版本TLS 1.1、TLS 1.2,几乎所有网站都使用这些版本。
这里是 android 版本列表和对 TLS 的支持。
不要尝试在 android webview 中处理 ssl 错误 url 否则 android 你的应用程序将被 Play 商店拒绝
不要尝试 handler.proceed();
这样做
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
Log.e("onReceivedSslError", "-- " + error.getUrl() + " " + error.getCertificate() + " " + view.getUrl());
showDialog(error.getUrl());
}
public void showDialog(String url) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Error");
alertDialog.setCancelable(false);
alertDialog.setMessage("Getting SSL Error open in other browser");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
openBrowser(WebViewActivity.this, url);
dialog.dismiss();
}
});
alertDialog.create().show();
}
public static void openBrowser(AppCompatActivity activity, String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (activity == null) return;
if (browserIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(Intent.createChooser(browserIntent, "Choose browser"));
} else {
showToast(activity, "No application can handle this request.\"\n" +
" + \" Please install a webbrowser");
}
}
我正在尝试从带有 WebView 库的 Android 应用程序访问网站,所以我有:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(
new SSLTolerentWebViewClient()
);
myWebView.loadUrl("https://www.mywebsite.ro");
}
}
class SSLTolerentWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if (error.toString() == "piglet")
handler.cancel();
else
handler.proceed(); // Ignore SSL certificate errors
}
}
在调试中我有:
D/OpenSSLLib: OpensslErr:Module:20(119:258); file:external/openssl/ssl/s23_clnt.c ;Line:714;Function:ssl23_get_server_hello
W/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:171 [0209/105028:WARNING:ssl_client_socket_openssl.cc(171)] Unmapped error reason: 258
E/chromium: external/chromium/net/socket/ssl_client_socket_openssl.cc:792 [0209/105028:ERROR:ssl_client_socket_openssl.cc(792)] handshake failed; returned -1, SSL error code 1, net_error -2
D/chromium/tcp: LogConnectCompletion with error:-2
D/WebRequest: request error: -2
D/chromium: Unknown chromium error: -2
V/webview: setCertificate=null
V/webcore: setupViewport mRestoredScale=0.0 mViewScale=2.0 mTextWrapScale=2.0 data.mScale= 2.0
V/webcore: viewSizeChanged w=360; h=559; textwrapWidth=360; scale=2.0
D/skia: Flag is not 10
如果您没有将您的应用上传到 Play 商店,您可以忽略 SSL 证书错误:
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}
这个问题是因为4.4以下的android版本不支持新版本TLS 1.1、TLS 1.2,几乎所有网站都使用这些版本。
这里是 android 版本列表和对 TLS 的支持。
不要尝试在 android webview 中处理 ssl 错误 url 否则 android 你的应用程序将被 Play 商店拒绝
不要尝试 handler.proceed();
这样做
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
Log.e("onReceivedSslError", "-- " + error.getUrl() + " " + error.getCertificate() + " " + view.getUrl());
showDialog(error.getUrl());
}
public void showDialog(String url) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Error");
alertDialog.setCancelable(false);
alertDialog.setMessage("Getting SSL Error open in other browser");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
openBrowser(WebViewActivity.this, url);
dialog.dismiss();
}
});
alertDialog.create().show();
}
public static void openBrowser(AppCompatActivity activity, String url) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (activity == null) return;
if (browserIntent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivity(Intent.createChooser(browserIntent, "Choose browser"));
} else {
showToast(activity, "No application can handle this request.\"\n" +
" + \" Please install a webbrowser");
}
}