我知道之前有人问过这个问题。但我真的不知道该怎么做。
我在 tomcat 服务器中创建了一个 j2me 应用程序和一个网络服务,它在 HTTP 上运行完美,现在我需要连接到 HTTPS/TLS。
为此,我遵循了以下步骤:
- 我使用 KeyTool 在服务器端生成一个 .keystore 文件。
- 我在 tomcat 中(在网络浏览器中)正确运行 HTTPS。
- 我获取了 .keystore 并将其导入到我的 WTK keystore 中。通过使用 Mekeytool。
当我尝试连接到我的 https://server-ip/webservice 时,使用这段代码:
public class HttpsMIDlet extends MIDlet implements CommandListener, Runnable {
private Display mDisplay;
private Form mForm;
public void startApp() {
mDisplay = Display.getDisplay(this);
if (mForm == null) {
mForm = new Form("HttpsMIDlet");
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
mForm.addCommand(new Command("Send", Command.SCREEN, 0));
mForm.setCommandListener(this);
}
mDisplay.setCurrent(mForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT)
notifyDestroyed();
else {
Form waitForm = new Form("Connecting...");
mDisplay.setCurrent(waitForm);
Thread t = new Thread(this);
t.start();
}
}
public void run() {
String url = "https://server-ip/webserive";
try {
// Query the server and retrieve the response.
HttpsConnection hc = (HttpsConnection) Connector.open(url);
SecurityInfo si = hc.getSecurityInfo();
Certificate c = si.getServerCertificate();
String subject = c.getSubject();
System.out.println();
String s = "Server certificate subject: \n" + subject;
Alert a = new Alert("Result", s, null, null);
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
hc.close();
} catch (IOException ioe) {
Alert a = new Alert("Exception", ioe.toString(), null, null);
System.out.println(ioe.toString());
a.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(a, mForm);
}
}
它抛出这个异常:
java.io.IOException: Bad record type (21) or version (3.3)
at com.sun.midp.ssl.Record.rdRec(+284)
at com.sun.midp.ssl.Record.rdRec(+5)
at com.sun.midp.ssl.Handshake.getNextMsg(+17)
at com.sun.midp.ssl.Handshake.rcvSrvrHello(+5)
at com.sun.midp.ssl.Handshake.doHandShake(+29)
at com.sun.midp.ssl.SSLStreamConnection.<init>(+173)
at com.sun.midp.ssl.SSLStreamConnection.<init>(+12)
at com.sun.midp.io.j2me.https.Protocol.connect(+214)
at com.sun.midp.io.j2me.http.Protocol.streamConnect(+57)
at com.sun.midp.io.j2me.http.Protocol.startRequest(+12)
at com.sun.midp.io.j2me.http.Protocol.sendRequest(+38)
at com.sun.midp.io.j2me.http.Protocol.sendRequest(+6)
at com.sun.midp.io.j2me.http.Protocol.openInputStream(+9)
请帮忙。谢谢。
最佳答案
通过搜索超过 15 天,通过为 tomcat 和 JDK 安装旧版本解决了这个异常问题。
这可能发生在处理 HTTPS/SSL 连接的 WTK 和 tomcat/JDK 版本的区分中。
适用于 tomcat 1.5
Java 版本 JKD 1.5_0_u7
我希望这可以帮助其他人。
关于java - 使用自签名证书将 J2ME 应用程序连接到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318651/