当前位置: 代码迷 >> Java Web开发 >> 处置https访问时URL.openConnection()返回com.sun.net.ssl.HttpsURLConnection,各位大侠请帮忙看看
  详细解决方案

处置https访问时URL.openConnection()返回com.sun.net.ssl.HttpsURLConnection,各位大侠请帮忙看看

热度:3963   发布时间:2013-02-25 21:10:10
处理https访问时URL.openConnection()返回com.sun.net.ssl.HttpsURLConnection,各位大侠请帮忙看看
测试环境:
JDK 1.4_2 操作系统:win2000 应用服务器:WebSphere 5.1.0

生产环境
JKD 1.6_0_14 操作系统:linux 应用服务器:Jboss 

发送https代码如下:

Java code
    public String postHttps(String urlString, String data, Logger logger) throws Exception {        SSLContext sc = null;        TrustManager[] trustAllCerts = new TrustManager[] { new javax.net.ssl.X509TrustManager() {            public java.security.cert.X509Certificate[] getAcceptedIssuers() {                return null;            }            public void checkClientTrusted(                    java.security.cert.X509Certificate[] certs, String authType) {            }            public void checkServerTrusted(                    java.security.cert.X509Certificate[] certs, String authType) {            }        } };                try {            sc = SSLContext.getInstance("SSL");            sc.init(null, trustAllCerts, new java.security.SecureRandom());            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());        } catch (Exception e) {        }                HostnameVerifier hv = new HostnameVerifier() {            public boolean verify(String urlHostName, SSLSession session) {                return urlHostName.equals(session.getPeerHost());            }        };        HttpsURLConnection.setDefaultHostnameVerifier(hv);        OutputStreamWriter os = null;        BufferedReader reader = null;        StringBuffer sb = new StringBuffer();        URL u = new URL(urlString);        HttpsURLConnection conn = (HttpsURLConnection) u.openConnection();        // conn.setRequestProperty("Content-Type", "text/xml");        conn.setDoOutput(true);        conn.setFollowRedirects(true);        // conn.setReadTimeout(30000);        os = new OutputStreamWriter(conn.getOutputStream());        os.write(data);        os.flush();        os.close();        return null;    }



在测试环境是可以正常的发送到指定的https地址上,而在生产环境上访问时会抛出以下错误:
java.lang.ClassCastException:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOLDImpl cannot be case to javax.net.ssl.HttpsURLConnection.


是在 HttpsURLConnection conn = (HttpsURLConnection) u.openConnection(); 处抛出。


之后用一下页面测试了下:

Java code
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%><%@ page import="java.net.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GBK"><title>测试Https</title></head><body><%    out.print("默认:<br/>");    URL aURL = new URL("https://www.verisign.com");        URLConnection urlConn = aURL.openConnection();    if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) {        out.print("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection<br/>");    }    if (urlConn instanceof javax.net.ssl.HttpsURLConnection) {        out.print("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection<br/>");    }    if (urlConn instanceof HttpURLConnection) {        out.print("*** openConnection returns an instnace of HttpURLConnection<br/>");    }%></body></html>



测试环境输出:
*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection
*** openConnection returns an instnace of HttpURLConnection
生产环境输出:
*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!