首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

midlet中应用https完全指南

2012-09-01 
midlet中使用https完全指南网上搜索了半天,都比较凌乱,花了好几个小时才搞定,写下来备忘,同时供大家参考!1

midlet中使用https完全指南

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

1、生成一个证书

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

      ??????????????
        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

        ?

        ?

        1 楼 redduke1202 2007-12-07   MIDlet开发,就要以部署到手机上为目标.而不是仅仅在模拟器上运行就好了
        1.手机上貌似导入证书还比较麻烦
        2.https依赖于长连接,而目前中移动的网络环境下,不存在长连接,无法使用https 2 楼 wuhua 2008-01-21   redduke1202 写道MIDlet开发,就要以部署到手机上为目标.而不是仅仅在模拟器上运行就好了
        1.手机上貌似导入证书还比较麻烦
        2.https依赖于长连接,而目前中移动的网络环境下,不存在长连接,无法使用https
        证书的安装还是不麻烦的。

热点排行