建立HttpsConnection
1建立HttpConnection,这种连接比较简单,但是是不安全的,网上例子比较多,现在主要说说如果建立HttpsConnection,这种连接时通过SSL协议加密,相对更安全,一般使用这种连接传输用户名密码,等重要信息的,下面看代码:
- public HttpsURLConnection getHttpsConnection(){
- try{
- TrustManager[] tm = { new MyX509TrustManager() };
- SSLContext sslContext = SSLContext.getInstance("SSL");
- sslContext.init(null, tm, new java.security.SecureRandom());
- SSLSocketFactory ssf = sslContext.getSocketFactory();
- System.out.println(TAG+" getHttpsConnection serverUrl="+serverUrl);
- URL myURL = new URL(serverUrl);
- HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
- httpsConn.setSSLSocketFactory(ssf);
- httpsConn.setRequestProperty("accept", "*/*");
- httpsConn.setRequestProperty("connection", "Keep-Alive");
- httpsConn.setRequestMethod("POST");
- httpsConn.setDoOutput(true);
- httpsConn.setDoInput(true);
- httpsConn.connect();
- return httpsConn;
- }catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
在上面的代码中比较重要的是MyX509TrustManager这个类,这个类是安全的保障,可以是用默认的,但是一般我们都需要对证书进行自定义,因此需要继承下,下面看代码:
- package login;
- import java.io.FileInputStream;
- import java.security.KeyStore;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.TrustManagerFactory;
- import javax.net.ssl.X509TrustManager;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class MyX509TrustManager implements X509TrustManager {
- //private final String CER_NAME = "D:\\Apache_Software_Foundation\\Tomcat_6.0\\cas.keystore";
- private final String CER_NAME ="D:\\apache-tomcat-7.0.42\\cas.keystore";
- private final String CER_PASSWORD = "changeit";
- private final Log logger = LogFactory.getLog(getClass());
- /*
- * The default X509TrustManager returned by SunX509. We'll delegate
- * decisions to it, and fall back to the logic in this class if the
- * default X509TrustManager doesn't trust it.
- */
- X509TrustManager sunJSSEX509TrustManager;
- MyX509TrustManager() throws Exception {
- // create a "default" JSSE X509TrustManager.
- KeyStore ks = KeyStore.getInstance("JKS");
- FileInputStream fis = new FileInputStream(CER_NAME);
- ks.load(fis,CER_PASSWORD.toCharArray());
- TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
- tmf.init(ks);
- TrustManager tms [] = tmf.getTrustManagers();
- for (int i = 0; i < tms.length; i++) {
- if (tms[i] instanceof X509TrustManager) {
- sunJSSEX509TrustManager = (X509TrustManager) tms[i];
- return;
- }
- }
- throw new Exception("liqingguo Couldn't initialize");
- }
- /*
- * Delegate to the default trust manager.
- */
- public void checkClientTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
- try {
- sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
- } catch (CertificateException excep) {
- // do any special handling here, or rethrow exception.
- }
- }
- /*
- * Delegate to the default trust manager.
- */
- public void checkServerTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
- try {
- sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
- } catch (CertificateException excep) {
- /*
- * Possibly pop up a dialog box asking whether to trust the
- * cert chain.
- */
- }
- }
- /*
- * Merely pass this through.
- */
- public X509Certificate[] getAcceptedIssuers() {
- return sunJSSEX509TrustManager.getAcceptedIssuers();
- }
- }
建立HttpsConnection的更多相关文章
- [原] 利用 OVS 建立 VxLAN 虚拟网络实验
OVS 配置 VxLAN HOST A ------------------------------------------ | zh-veth0(10.1.1.1) VM A | | ---|--- ...
- Atitit.如何建立研发体系
Atitit.如何建立研发体系 组织,流程,prj..Mana oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...
- 在 Linux 上使用 Jexus + Mono 建立 Asp.Net 网站.
godaddy 买了个net空间,一点也不好用. 几个G的数据, 上传数据只有几kb , 想用 ssh 登录上去用 wget 下载,也不行 windows的主机貌似没有 ssh 功能... 后来实在忍 ...
- ASP.NET Core管道深度剖析(4):管道是如何建立起来的?
在<管道是如何处理HTTP请求的?>中,我们对ASP.NET Core的请求处理管道的构成以及它对请求的处理流程进行了详细介绍,接下来我们需要了解的是这样一个管道是如何被构建起来的.这样一 ...
- 读书笔记--SQL必知必会--建立练习环境
书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL in 10 Minutes - Fourth Edition> MyS ...
- 【转】SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误
SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误 最近在VS2013上连接远程数据库时,突然连接不上,在跑MSTest下跑的时候,QTAgent32 crash.换成IIS ...
- 黑马程序员——ARC机制总结和用ARC建立模型
ARC 全称:Automatic Reference Counting 使用ARC 只需要在建立一个新的项目的时候把 下面的√打上 Xcode5以后都会默认建议开发者使用ARC机制 新的项目中如果有部 ...
- gRPC源码分析2-Server的建立
gRPC中,Server.Client共享的Class不是很多,所以我们可以单独的分别讲解Server和Client的源码. 通过第一篇,我们知道对于gRPC来说,建立Server是非常简单的,还记得 ...
- word 2010 建立多级结构和目录
点击“开始”中的“样式”中右下角按钮 点击弹出窗口中最下方第三个按钮 点击“推荐”选项卡,再选择要显示的标题,然后点击“显示”.然后按照需要,分别把“标题3”“标题4”等显示.最后点确定即可. ...
随机推荐
- [原创]ie6,7中td和img之间有间隙
情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...
- WPF中窗口控件的跨线程调用
在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...
- android自定义样式大全:shape,selector,layer-list,style,动画全部内容
原文:http://keeganlee.me/post/android/20150830 以下摘取了部分内容: shape 一般用shape定义的xml文件存放在drawable目录下,若项目没有该目 ...
- C#字符串颠倒输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- 朋友的礼物(英雄会,csdn,高校俱乐部)信封问题,匹配模型
前言: 首先这是一题解,但是重点最代码之后,有耐心的可以直接从代码后看. 上题目:n个人,每个人都有一件礼物想送给他人,他们决定把礼物混在一起,然后每个人随机拿走一件,问恰好有m个人拿到的礼物恰好是自 ...
- VC++ 17、18课
进程间通信的四种方式: 剪贴板 匿名管道 命名管道 邮槽 容器和服务器程序 容器应用程序是可以嵌入或链接对象的应用程序.word就是容器应用程序. 服务器应用程序是创建对象并且当对象呗双击时,可以被启 ...
- TCP/IP状态转换图
- 24种设计模式--模版方法模式【Template Method Pattern】
周三,9:00,我刚刚坐到位置,打开电脑准备开始干活.“小三,小三,叫一下其它同事,到会议室,开会”老大跑过来吼,带着淫笑.还不等大家坐稳,老大就开讲了,“告诉大家一个好消息,昨天终于把牛叉模型公司的 ...
- APACHE 与IIS同时存在的情况下,给APACHE添加反向代理 共用80端口
一.首先打开IIS,将IIS的端口改成81,不要让IIS占用了80端口 二.打开APACHE的httpd.conf配置文件,将里面的端口配置成80 三.打开APACHE的虚拟目录配置文件,如:http ...
- Ubuntu 之旅 —— 解决sudo: source: command not found错误
$ sudo -s # source /etc/profile