1建立HttpConnection,这种连接比较简单,但是是不安全的,网上例子比较多,现在主要说说如果建立HttpsConnection,这种连接时通过SSL协议加密,相对更安全,一般使用这种连接传输用户名密码,等重要信息的,下面看代码:

  1. public HttpsURLConnection getHttpsConnection(){
  2. try{
  3. TrustManager[] tm = { new MyX509TrustManager() };
  4. SSLContext sslContext = SSLContext.getInstance("SSL");
  5. sslContext.init(null, tm, new java.security.SecureRandom());
  6. SSLSocketFactory ssf = sslContext.getSocketFactory();
  7. System.out.println(TAG+" getHttpsConnection serverUrl="+serverUrl);
  8. URL myURL = new URL(serverUrl);
  9. HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
  10. httpsConn.setSSLSocketFactory(ssf);
  11. httpsConn.setRequestProperty("accept", "*/*");
  12. httpsConn.setRequestProperty("connection", "Keep-Alive");
  13. httpsConn.setRequestMethod("POST");
  14. httpsConn.setDoOutput(true);
  15. httpsConn.setDoInput(true);
  16. httpsConn.connect();
  17. return httpsConn;
  18. }catch(Exception e){
  19. e.printStackTrace();
  20. return null;
  21. }
  22.  
  23. }

在上面的代码中比较重要的是MyX509TrustManager这个类,这个类是安全的保障,可以是用默认的,但是一般我们都需要对证书进行自定义,因此需要继承下,下面看代码:

  1. package login;
  2. import java.io.FileInputStream;
  3. import java.security.KeyStore;
  4. import java.security.cert.CertificateException;
  5. import java.security.cert.X509Certificate;
  6.  
  7. import javax.net.ssl.TrustManager;
  8. import javax.net.ssl.TrustManagerFactory;
  9. import javax.net.ssl.X509TrustManager;
  10.  
  11. import org.apache.commons.logging.Log;
  12. import org.apache.commons.logging.LogFactory;
  13.  
  14. public class MyX509TrustManager implements X509TrustManager {
  15. //private final String CER_NAME = "D:\\Apache_Software_Foundation\\Tomcat_6.0\\cas.keystore";
  16. private final String CER_NAME ="D:\\apache-tomcat-7.0.42\\cas.keystore";
  17. private final String CER_PASSWORD = "changeit";
  18. private final Log logger = LogFactory.getLog(getClass());
  19.  
  20. /*
  21. * The default X509TrustManager returned by SunX509. We'll delegate
  22. * decisions to it, and fall back to the logic in this class if the
  23. * default X509TrustManager doesn't trust it.
  24. */
  25. X509TrustManager sunJSSEX509TrustManager;
  26.  
  27. MyX509TrustManager() throws Exception {
  28. // create a "default" JSSE X509TrustManager.
  29. KeyStore ks = KeyStore.getInstance("JKS");
  30. FileInputStream fis = new FileInputStream(CER_NAME);
  31. ks.load(fis,CER_PASSWORD.toCharArray());
  32. TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
  33. tmf.init(ks);
  34. TrustManager tms [] = tmf.getTrustManagers();
  35. for (int i = 0; i < tms.length; i++) {
  36. if (tms[i] instanceof X509TrustManager) {
  37. sunJSSEX509TrustManager = (X509TrustManager) tms[i];
  38. return;
  39. }
  40. }
  41. throw new Exception("liqingguo Couldn't initialize");
  42. }
  43.  
  44. /*
  45. * Delegate to the default trust manager.
  46. */
  47. public void checkClientTrusted(X509Certificate[] chain, String authType)
  48. throws CertificateException {
  49. try {
  50. sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
  51. } catch (CertificateException excep) {
  52. // do any special handling here, or rethrow exception.
  53. }
  54. }
  55.  
  56. /*
  57. * Delegate to the default trust manager.
  58. */
  59. public void checkServerTrusted(X509Certificate[] chain, String authType)
  60. throws CertificateException {
  61. try {
  62. sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
  63. } catch (CertificateException excep) {
  64. /*
  65. * Possibly pop up a dialog box asking whether to trust the
  66. * cert chain.
  67. */
  68. }
  69. }
  70.  
  71. /*
  72. * Merely pass this through.
  73. */
  74. public X509Certificate[] getAcceptedIssuers() {
  75. return sunJSSEX509TrustManager.getAcceptedIssuers();
  76. }
  77. }

建立HttpsConnection的更多相关文章

  1. [原] 利用 OVS 建立 VxLAN 虚拟网络实验

    OVS 配置 VxLAN HOST A ------------------------------------------ | zh-veth0(10.1.1.1) VM A | | ---|--- ...

  2. Atitit.如何建立研发体系

    Atitit.如何建立研发体系 组织,流程,prj..Mana  oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...

  3. 在 Linux 上使用 Jexus + Mono 建立 Asp.Net 网站.

    godaddy 买了个net空间,一点也不好用. 几个G的数据, 上传数据只有几kb , 想用 ssh 登录上去用 wget 下载,也不行 windows的主机貌似没有 ssh 功能... 后来实在忍 ...

  4. ASP.NET Core管道深度剖析(4):管道是如何建立起来的?

    在<管道是如何处理HTTP请求的?>中,我们对ASP.NET Core的请求处理管道的构成以及它对请求的处理流程进行了详细介绍,接下来我们需要了解的是这样一个管道是如何被构建起来的.这样一 ...

  5. 读书笔记--SQL必知必会--建立练习环境

    书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL in 10 Minutes - Fourth Edition> MyS ...

  6. 【转】SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误

    SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误 最近在VS2013上连接远程数据库时,突然连接不上,在跑MSTest下跑的时候,QTAgent32 crash.换成IIS ...

  7. 黑马程序员——ARC机制总结和用ARC建立模型

    ARC 全称:Automatic Reference Counting 使用ARC 只需要在建立一个新的项目的时候把 下面的√打上 Xcode5以后都会默认建议开发者使用ARC机制 新的项目中如果有部 ...

  8. gRPC源码分析2-Server的建立

    gRPC中,Server.Client共享的Class不是很多,所以我们可以单独的分别讲解Server和Client的源码. 通过第一篇,我们知道对于gRPC来说,建立Server是非常简单的,还记得 ...

  9. word 2010 建立多级结构和目录

    点击“开始”中的“样式”中右下角按钮   点击弹出窗口中最下方第三个按钮   点击“推荐”选项卡,再选择要显示的标题,然后点击“显示”.然后按照需要,分别把“标题3”“标题4”等显示.最后点确定即可. ...

随机推荐

  1. [原创]ie6,7中td和img之间有间隙

    情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...

  2. WPF中窗口控件的跨线程调用

    在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...

  3. android自定义样式大全:shape,selector,layer-list,style,动画全部内容

    原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...

  4. C#字符串颠倒输出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  5. 朋友的礼物(英雄会,csdn,高校俱乐部)信封问题,匹配模型

    前言: 首先这是一题解,但是重点最代码之后,有耐心的可以直接从代码后看. 上题目:n个人,每个人都有一件礼物想送给他人,他们决定把礼物混在一起,然后每个人随机拿走一件,问恰好有m个人拿到的礼物恰好是自 ...

  6. VC++ 17、18课

    进程间通信的四种方式: 剪贴板 匿名管道 命名管道 邮槽 容器和服务器程序 容器应用程序是可以嵌入或链接对象的应用程序.word就是容器应用程序. 服务器应用程序是创建对象并且当对象呗双击时,可以被启 ...

  7. TCP/IP状态转换图

  8. 24种设计模式--模版方法模式【Template Method Pattern】

    周三,9:00,我刚刚坐到位置,打开电脑准备开始干活.“小三,小三,叫一下其它同事,到会议室,开会”老大跑过来吼,带着淫笑.还不等大家坐稳,老大就开讲了,“告诉大家一个好消息,昨天终于把牛叉模型公司的 ...

  9. APACHE 与IIS同时存在的情况下,给APACHE添加反向代理 共用80端口

    一.首先打开IIS,将IIS的端口改成81,不要让IIS占用了80端口 二.打开APACHE的httpd.conf配置文件,将里面的端口配置成80 三.打开APACHE的虚拟目录配置文件,如:http ...

  10. Ubuntu 之旅 —— 解决sudo: source: command not found错误

    $ sudo -s # source /etc/profile