Android(api 21+)Webview无法访问本地IP地址,例如:172.18.17.16.5432
尝试了许多解决方案,例如 Trusting all certificates using HttpClient over HTTPS 在互联网上,但没有答案。
我们有: 1.自己的证书。 2.本地服务器,如171.18.13.12:3456 3.它可以从PC(例如chrome)启用,但表示不安全(https交叉地址) 4.我们还有webview android。 5.然后我们应该在服务器上。
但不是。
webView.setBackgroundColor(Color.TRANSPARENT);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setDomStorageEnabled(true);
String newUA = "clientName";
webView.getSettings().setUserAgentString(newUA);
if (!webView.equals(webViewLocal))
webView.setWebViewClient(new WebClient(webViewLocal, this));
else {
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog(view, true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
progressDialog(view, false);
}
});
}
webView.setWebChromeClient(new ChromeClient());
webView.getSettings().setUseWideViewPort(false);
webView.getSettings().setLoadWithOverviewMode(false);
信任:
private void addToTrustStore(KeyStore trustStore) {
KeyManagerFactory kmfactory = null;
try {
kmfactory = KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(trustStore, pass_cert.toCharArray());
KeyManager[] keymanagers = kmfactory.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keymanagers, tmf.getTrustManagers(), new SecureRandom());
SSLContext.setDefault(sslContext);
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException | KeyManagementException e) {
e.printStackTrace();
}
}
private void loadCertificateAndPrivateKey() {
try {
InputStream certificateFileStream = getClass().getResourceAsStream("/assets/" + cert_file);
KeyStore keyStore = KeyStore.getInstance("SomeName");
keyStore.load(certificateFileStream, pass_store != null ? pass_store.toCharArray() : null);
addToTrustStore(keyStore);
String alias = "someName";
Key key = keyStore.getKey(alias, pass_cert.toCharArray());
if (key instanceof PrivateKey) {
mPrivateKey = (PrivateKey) key;
Certificate cert = keyStore.getCertificate(alias);
mCertificates = new X509Certificate[1];
mCertificates[0] = (X509Certificate) cert;
}
certificateFileStream.close();
} catch (Exception e) {
Log.e(LOG_TAG, e.getMessage());
}
}
应用程序也会抛出
io.netty.handler.codec.DecoderException: javax.net.ssl.SSLException: Received fatal alert: certificate_unknown
问题是:是否可以通过某种方式在不注册域的情况下访问我的服务器?如果是,请帮助。 还是我做错了?