Java Secure Socket Extension (JSSE) - HttpsURLConnection class cast exception problem

Hy everyone,

I have next java code to send xml file over https.

// Create a trust manager that does not validate certificate chains

TrustManager[] trustAllCerts = new TrustManager[] {

new 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) {

}

}

};

//Install the all-trusting trust manager

try {

SSLContext sc = SSLContext.getInstance("SSL");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

} catch (Exception e) {

e.printStackTrace();

}

//Make the output file for xml response

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_kk-mm-ss");

_xmlResponseIme = dateFormat.format(new Date());

try {

if (createOutXML(_xmlResponseIme)){

this.set_xmlResponseIme(_xmlResponseIme);

}

} catch (IOException e2) {

//System.out.println("SSLClient :: createOutXML catch");

e2.printStackTrace();

}

//translate request.xml file to String

File xmlRequest = new File(_path + "/XMLFiles/request.xml");

String xmlFile="";

try {

xmlFile = this.getStringFromXMLFile(xmlRequest);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

this.registerMyHostnameVerifier();

try {

path = "https://"host_config.getString("path");

url = new URL(path);

} catch (MalformedURLException e1) {

e1.printStackTrace();

}

try {

conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=Cp1250");

conn.setDoInput(true);

conn.setDoOutput(true);

//conn.setDefaultUseCaches(false);

conn.setRequestProperty("xml", xmlFile);

conn.setAllowUserInteraction(true);

System.out.println("Response kod = " + conn.getResponseCode());

} catch (IOException e2) {

e2.printStackTrace();

}

try {

//receive answer

InputStream in = conn.getInputStream();

FileOutputStream fout;

fout = new FileOutputStream(_xmlOut);

StringBuffer sb = new StringBuffer();

Reader reader = new InputStreamReader(in, "Cp1250");

int c;

while ((c = in.read()) != -1){

sb.append((char) c);

fout.write((byte) c);

}

String document = sb.toString();

System.out.println(document);

fout.close();

in.close();

} catch (IOException e3) {

e3.printStackTrace();

}

Problem is that on one WebSphere Application Server 6 is everithing OK, but on another I receive error :

On both server I have same jdk.

Error 500: com.ibm.net.ssl.internal.www.protocol.https.HttpsURLConnection

in line

conn = (javax.net.ssl.HttpsURLConnection) url.openConnection();

Where is the problem ?

[3385 byte] By [knovaka] at [2007-11-26 23:02:38]
# 1
Problem is that the IBM application has its own URLConnection implementation and using the default API's results in casting it to HttpsURLConnection.
Imiroa at 2007-7-10 13:53:19 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 2
Thx for reply. How to avoid this ? And way is everything OK on the test server (localhost) and when import ear to the production server everything goes wrong?
knovaka at 2007-7-10 13:53:19 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 3

I've seen a similar situation where you instantiate the com.ibm.httpsurlconnection manually, but you probably have to check out ibm documentation.

Test server might have different libraries / settings that affect this. Production may have a different ibm built jdk etc.. Make a .getClass().getName() out of the connection object before you actually cast it to anything.

Imiroa at 2007-7-10 13:53:19 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...
# 4
Very interesting.When I check url.openConnection().getClass().getName() i received next resultfor test server:com.ibm.net.ssl.www.protocol.https.tfor production server:com.ibm.net.ssl.internal.www.protocol.https.HttpsURLConnection
knovaka at 2007-7-10 13:53:19 > top of Java-index,Security,Java Secure Socket Extension (JSSE)...