使用自签名证书将J2ME应用程序连接到https [英] connect J2ME application to https with self-signed certificate

查看:89
本文介绍了使用自签名证书将J2ME应用程序连接到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题曾被问过.但我真的不知道该怎么做.

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

为此,我按照以下步骤操作:

  1. 使用KeyTool我在服务器端生成一个.keystore文件.
  2. 我在tomcat(在网络浏览器中)正确运行HTTPS.
  3. 我使用了.keystore并将其导入到我的WTK密钥存储区中.通过使用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天,此 Exception 问题通过为Tomcat和JDK安装了旧版本得以解决. >

这可能是WTK和tomcat/JDK版本的区别,后者处理HTTPS/SSL连接.

  1. for tomcat 1.5
  2. for java version JKD 1.5_0_u7

我希望这可以对其他人有所帮助.

I know this question was asked before. but i really don't know how to do this.

I created a j2me app and a webservice in a tomcat server, it works perfect on HTTP, Now i need to connect to HTTPS/TLS.

To do that i followed this steps:

  1. using KeyTool i generate a .keystore file in the server–side.
  2. i run HTTPS correctly in tomcat (in web browser).
  3. i took the .keystore and i import it to my WTK keys stores. by using Mekeytool.

When i try to connect to my https://server-ip/webservice, using this snippet of code:

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);
    }
}

It throws this Exception:

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)

Some help please. thank you.

解决方案

By doing search for more than 15 days, this Exception issue solved by installing an old versions for both tomcat and JDK.

This may be occurred in differentiation of the WTK and tomcat/JDK versions, which handle the HTTPS/SSL connection.

  1. for tomcat 1.5
  2. for java version JKD 1.5_0_u7

I hope this may help others.

这篇关于使用自签名证书将J2ME应用程序连接到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆