[英]connect J2ME application to https with self-signed certificate
我知道这个问题曾被问过。 但我真的不知道该怎么做。
我在tomcat服务器中创建了一个j2me应用程序和一个Web服务,它在HTTP上完美运行, 现在我需要连接到HTTPS / TLS。
为此,我按照以下步骤操作:
当我尝试使用以下代码片段连接到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安装了旧版本而得以解决。
这可能是WTK和tomcat / JDK版本的区别,后者处理HTTPS / SSL连接。
for tomcat 1.5
for java version JKD 1.5_0_u7
我希望这可以帮助其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.