HttpsURLConnection class cast exception problem
843811Mar 27 2007 — edited Jun 19 2008Hy 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 ??