我尝试使用NanoHTTPD在Android中运行HTTPS服务器,并且我正在获取IOException"密钥存储的错误版本"。解释:
当使用HTTP(无HTTPS)时,一切正常,我的代码:
try {
WebServer webServer = new WebServer(8080);
webServer.start();
} catch (Exception e) {
e.printStackTrace();
}
就像文档说的那样,我用以下方式生成证书:
keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass mypassword -validity 360 -keysize 2048 -ext SAN=DNS:localhost,IP:127.0.0.1 -validity 9999
测试1(没有任何反应):
try {
WebServer webServer = new WebServer(443);
webServer.makeSecure(NanoHTTPD.makeSSLSocketFactory("src/main/resources/keystore.jks",
"mypassword".toCharArray()), null);
webServer.start();
} catch (Exception e) {
e.printStackTrace();
}
测试2(IOException):
try {
NanoHTTPD secureAppServer = new WebServer(9043);
File f = new File("src/main/resources/keystore.jks");
System.setProperty("javax.net.ssl.trustStore", f.getAbsolutePath());
secureAppServer.setServerSocketFactory(new NanoHTTPD.SecureServerSocketFactory(
NanoHTTPD.makeSSLSocketFactory("/" + f.getName(), "mypassword".toCharArray()), null));
secureAppServer.start();
} catch (IOException e) {
Log.d("WebServer", "IOException e: " + e.getMessage());
e.printStackTrace();
}
有什么想法吗?也许我可以通过其他方式修复此问题生成证书,但我不知道如何。
谢谢!