midlet中使用https完全指南

网上搜索了半天,都比较凌乱,花了好几个小时才搞定,写下来备忘,同时供大家参考!

1、生成一个证书

使用jdk自带的工具keytool来生成证书,假设jdk安装路径为d:\jdk

java 代码
  1. cd D:\jdk\bin   
  2. keytool -genkey -alias tomcat -keyalg RSA -keystore c:\keystore   

按照提示回答问题,完了后在c盘根目录下就生成了证书文件keystore. 注意填写您的姓名的时候要填写网站的主机名,比如localhost。

 

2、在tomcat5中配置https

将server.xml中找到下面的代码,默认被注释掉了

xml 代码
  1. <Connector port="8443" maxHttpHeaderSize="8192"  
  2.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  3.                enableLookups="false" disableUploadTimeout="true"  
  4.                acceptCount="100" scheme="https" secure="true"  
  5.                clientAuth="false" sslProtocol="TLS" />    

替换为

xml 代码
  1. <Connector port="443" maxHttpHeaderSize="8192"  
  2.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  3.                enableLookups="true" disableUploadTimeout="true"  
  4.                acceptCount="100" scheme="https" secure="true"  
  5.                clientAuth="false" sslProtocol="TLS" keystoreFile="c:/keystore"
  6.                keystorePass="hxrainbow"/>   
 

 重新启动tomcat,使用https应该就能访问到了。

 3、导入证书到wtk

假设wtk的安装路径为D:\WTK2.5.2\

java 代码
  1. cd D:\WTK2.5.2\bin   
  2. mekeytool.exe -import -keystore c:\keystore -storepass hxrainbow  -alias tomcat  

没有错误提示,就导入成功。

4、在midlet中使用https

主要就是使用HttpsConnection,HttpsConnection是HttpConnection的子类,使用方法一样的,不再啰嗦了,给个帮助手册里面的代码

java 代码
  1. void getViaHttpsConnection(String url)    
  2.             throws CertificateException, IOException {   
  3.          HttpsConnection c = null;   
  4.          InputStream is = null;   
  5.          try {   
  6.              c = (HttpsConnection)Connector.open(url);   
  7.     
  8.              // Getting the InputStream ensures that the connection   
  9.              // is opened (if it was not already handled by   
  10.              // Connector.open()) and the SSL handshake is exchanged,   
  11.              // and the HTTP response headers are read.   
  12.              // These are stored until requested.   
  13.              is = c.openDataInputStream();   
  14.     
  15.              if c.getResponseCode() == HttpConnection.HTTP_OK) {   
  16.                  // Get the length and process the data   
  17.                  int len = (int)c.getLength();   
  18.                  if (len > 0) {   
  19.                      byte[] data = new byte[len];   
  20.                      int actual = is.readFully(data);   
  21.                      ...   
  22.                  } else {   
  23.                      int ch;   
  24.                      while ((ch = is.read()) != -1) {   
  25.                          ...   
  26.                      }   
  27.                  }   
  28.              } else {   
  29.                ...   
  30.              }   
  31.          } finally {   
  32.              if (is != null)   
  33.                  is.close();   
  34.              if (c != null)   
  35.                  c.close();   
  36.          }   
  37.      }   

 编写完成后,用模拟器应该就可以访问https了。

 

5、可能遇到的问题

1)运行midlet时,异常Certificate   was   issued   by   an   unrecognized   entity  

未将证书导入到wtk中

2)使用wtk界面来导入证书的时候,提示too big

换成文中描述的命令行方式来导入或者使用keytool导出一个cert证书

3)Certificate does not contain the correct site name

keystore里面的CN值不正确,重新生成keystore,CN填写为主机名,比如 127.0.0.1

4)删除证书

keytool  -delete -alias tomcat -keystore c:\keystore -storepass hxrainbow

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值