测试环境:
JDK 1.4_2 操作系统:win2000 应用服务器:WebSphere 5.1.0
生产环境
JKD 1.6_0_14 操作系统:linux 应用服务器:Jboss
发送https代码如下:
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(); 处抛出。
之后用一下页面测试了下:
<%@ 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
*** openConnection returns an instnace of HttpURLConnection
这就是问题所在,URL.openConnection() 在测试环境返回javax.net.ssl.HttpsURLConnection,而在生产环境则返回:com.sun.net.ssl.HttpsURLConnection
如何能将生产环境URL.openConnection()的返回从com.sun.net.ssl.HttpsURLConnection 变成javax.net.ssl.HttpsURLConnection ?
望做过Https访问的大侠们不吝赐教!
在线等 或联系qq 13386778 万分感谢!
------解决方案--------------------
javax.net.ssl.HttpsURLConnection
com.sun.net.ssl.HttpsURLConnection
com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOLDImpl
这些类貌似都是在jdk的jsse.jar中,而这个com.sun.net.ssl.HttpsURLConnection应该是deprecated的了,代码都一样吧,仅仅是环境不同,具体实现类的选择貌似视环境而定,感觉是环境高度相关的一个case,能不能先试试把测试环境的jsse.jar换到生成环境中...
------解决方案--------------------