java - 使用自签名证书将 J2ME 应用程序连接到 https

标签 java tomcat java-me midp

我知道之前有人问过这个问题。但我真的不知道该怎么做。

我在 tomcat 服务器中创建了一个 j2me 应用程序和一个网络服务,它在 HTTP 上运行完美,现在我需要连接到 HTTPS/TLS。

为此,我遵循了以下步骤:

  1. 我使用 KeyTool 在服务器端生成一个 .keystore 文件。
  2. 我在 tomcat 中(在网络浏览器中)正确运行 HTTPS。
  3. 我获取了 .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 版本的区分中。

  1. 适用于 tomcat 1.5
  2. Java 版本 JKD 1.5_0_u7

我希望这可以帮助其他人。

关于java - 使用自签名证书将 J2ME 应用程序连接到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318651/

相关文章:

java - 用于监视客户端资源的 JSF Web 应用程序

java - 通过 java 运行时使用 pickled python 类

java - SpringBoot jUnit 未加载 application-dev.xml

java - 当我编译它时,它显示有关 Product.java 的错误。但该文件正在工作并显示

blackberry - 在黑莓中从头开始启动媒体(音频+视频)

java - JTable 捕获关键事件?

tomcat - docker镜像中的tomcat文件夹在哪里

tomcat - netty-tcnative 不适用于 Google Cloud Vision API

java - j2me中如何将12小时时间转换为24小时时间?

java - J2ME 从 Vector 获取特定对象