标签:
注意一下文章中提到的jsse在jdk1.4以后已经集成了,不必纠结.
摘 要 JSSE是一个SSL和TLS的纯Java实现,通过JSSE可以很容易地编程实现对HTTPS站点的访问。但是,如果该站点的证书未经权威机构的验证,JSSE将拒绝信任该证书从而不能访问HTTPS站点。本文在简要介绍JSSE的基础上提出了两种解决该问题的方法。
URL reqURL = new URL("https://www.sun.com" ); //创建URL对象 HttpsURLConnection httpsConn = (HttpsURLConnection)reqURL.openConnection(); /*下面这段代码实现向Web页面发送数据,实现与网页的交互访问 httpsConn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(huc.getOutputStream(), "8859_1"); out.write( "……" ); out.flush(); out.close(); */ //取得该连接的输入流,以读取响应内容 InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(); //读取服务器的响应内容并显示 int respInt = insr.read(); while( respInt != -1){ System.out.print((char)respInt); respInt = insr.read(); } |
图1 部分JSSE类的关系图 |
//创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[] tm = {new MyX509TrustManager ()}; SSLContext sslContext = SSLContext.getInstance("SSL","SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); //从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = sslContext.getSocketFactory(); //创建HttpsURLConnection对象,并设置其SSLSocketFactory对象 HttpsURLConnection httpsConn = (HttpsURLConnection)myURL.openConnection(); httpsConn.setSSLSocketFactory(ssf); |
HTTPS的证书未经权威机构认证的情况下,访问HTTPS站点的两种方法
标签:
原文地址:http://my.oschina.net/swearyd7/blog/494100