java.io.IOException: Server returned HTTP response code 500
hi, i'm trying to send and receive soap messages between the local oracle Apache server and a remote server. The remote server is a service provider and i don't have details of it's configuration.
The program follows:
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.ext.*;
import javax.net.ssl.*;
import java.net.*;
import java.io.*;
import java.security.cert.X509Certificate;
class MySAXExampleHandler extends DefaultHandler {
private int flag = 0;
public String accData = new String();
public MySAXExampleHandler() {
}
public void startDocument() throws SAXException {
System.out.println("START DOCUMENT");
System.out.println("<?xml version=?.0?>");
}
public void endDocument () throws SAXException {
System.out.println("END DOCUMENT");
}
public void characters (char buf [],
int offset,
int len)
throws SAXException {
String s = new String(buf, offset, len);
if(flag == 1){
accData = accData + " " + s;
}
System.out.println (s);
}
public void startElement(String namespaceURI,
String localName,
String rawName,
Attributes attrs)
throws SAXException {
System.out.println("Start Element");
if(rawName == "connectionId"){
flag = 1;
}
System.out.print("<"+rawName+localName);
int length = attrs.getLength();
for (int i=0; i < length; i++) {
System.out.print(" "+attrs.getLocalName(i)+"="+attrs.getValue(i));
}
System.out.println(">");
}
public void endElement(String namespaceURI,
String localName,
String rawName)
throws SAXException {
System.out.println ("</"+rawName+">");
if(flag == 1){
flag = 0;
}
System.out.println("End Element");
}
}
public class MySAXExample {
//Data Variables
String QText = new String();
String AgentID = new String();
long Duration = 0;
String StartTime = new String();
String EndTime = new String();
String strBody = new String();
String strURL = new String();
String SessID = new String();
//IO Stream Variables
OutputStream aout;
OutputStreamWriter wout;
InputStream in;
String g2aData = new String();
//Parser Variables
SAXParserFactory factory;
SAXParser parser;
javax.net.ssl.HttpsURLConnection objHttp;
// Create a trust manager that does not validate certificate chains
static private TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
final java.security.cert.X509Certificate[] certs,
final String authType) {
}
public void checkServerTrusted(
final java.security.cert.X509Certificate[] certs,
final String authType) {
}
} };
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
public MySAXExample () {
}
private HttpsURLConnection makeConnection(String newStrMethod, String newStrRequestURL, String newStrBody){
//Initializing URL Connection.
try{
URL url = new URL(newStrRequestURL);
final SSLContext sslCon = SSLContext.getInstance("SSL"); //$NON-NLS-1$
sslCon.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslCon
.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URLConnection connection = url.openConnection();
//Setting URL Connection Property.
connection.setAllowUserInteraction(false);
//Typecasting URLConnection into a HttpsURLConnection
objHttp = (javax.net.ssl.HttpsURLConnection) connection;
// Specifying properties for HttpsURLConnection\
objHttp.setRequestProperty("cache-control", "no-cache" );
objHttp.setRequestProperty("SOAPAction", "");
objHttp.setRequestProperty("Pragma" , "no-cache" );
objHttp.setRequestProperty("accept-language", "en-us");
objHttp.setRequestProperty("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
objHttp.setRequestProperty("User-Agent", "IE4");
objHttp.setRequestProperty("Host", "www.gotoassist.com");
objHttp.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//objHttp.setRequestProperty("Content-Length", String.valueOf(newStrBody.length()));
//objHttp.setRequestMethod(newStrMethod);
objHttp.setDoOutput(true);
objHttp.setDoInput(true);
} catch (Exception ex){
System.out.println("Exception Occurred " + ex);
}
return objHttp;
}
private String G2A_Query(String newStrMethod, String newStrURL, String newStrBody) {
try {
System.setProperty("java.protocol.handler.pkgs","javax.net.ssl");
//Initializing SAX Parser
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
//Initializing the SAXHandler Class.
MySAXExampleHandler saxHandle = new MySAXExampleHandler();
//XML Query
strBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "\n"
+ " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "\n"
+" <soapenv:Body>" + "\n"
+ newStrBody + " </soapenv:Body>" + "\n"
+ "</soapenv:Envelope>";
//Making Connection to Go To Assist Server
HttpsURLConnection tempConn = makeConnection(newStrMethod, newStrURL, strBody);
System.out.println("Made Connection");
//Send Query
aout = tempConn.getOutputStream();
wout = new OutputStreamWriter(aout, "UTF-8");
wout.write(strBody);
//Closing OutPut Stream
wout.flush();
// Read the response
in = tempConn.getInputStream();
//Parsing the Input Received using a SAX Parser.
parser.parse(in, saxHandle);
//Getting any data accumulated
g2aData = saxHandle.accData;
//Closing the Input Stream
in.close();
wout.close();
//Closing the Https Connection
tempConn.disconnect();
} catch (FactoryConfigurationError fce) {
System.out.println("FactoryConfigurationError occurred : "+fce);
} catch (ParserConfigurationException pce) {
System.out.println("ParserConfigurationException occurred :"+pce);
} catch (IOException e) {
System.out.println("IOException Occurred : " + e);
e.printStackTrace();
} catch (Exception e) {
System.out.println("Exception Occurred : " + e);
}
return g2aData;
}
public void dataComn(){
String DTSUserName = "someusername";
String DTSPassword = "somepassword";
String DTSSessionID = "somesessid";
//Assorted Session ID's for testing
//"41042471";//"34535409"; //"34535921"; //"34535409"; //"34534022";
//"34533669"; //"34533458"; //"34149473";//"34532898";
//The Go To Assist URL.
strURL = "https://someSSLURL";
strBody = "<ns1:logon soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"G2A_Sessions\">" + "\n"
+"<id xsi:type=\"xsd:string\">" + DTSUserName + "</id>" + "\n"
+"<password xsi:type=\"xsd:string\">" + DTSPassword + "</password>" + "\n"
+"<version xsi:type=\"xsd:long\">1</version>" + "\n"
+"</ns1:logon>" + "\n";
//Sending DTSUserName, DTSPassword and Querying for Connection ID
SessID = G2A_Query("GET", strURL ,strBody);
if(SessID.length() > 0){
System.out.println("SessID " + SessID.trim());
//Sending DTSSessionID and ConnectionID and Querying for DTS Session Details
strBody = "<ns1:getSessionById soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"G2A_Sessions\">" + "\n" +
"<connectionId xsi:type=\"xsd:string\">" + SessID + "</connectionId>" + "\n" +
"<sessionId xsi:type=\"xsd:long\">" + DTSSessionID + "</sessionId>" + "\n" +
"</ns1:getSessionById>" + "\n";
G2A_Query("GET", strURL, strBody);
}
}
public static void main(String[] args){
MySAXExample mySax = new MySAXExample();
mySax.dataComn();
}
}
The following is the output:
- JAVA -
Made Connection
START DOCUMENT
<?xml version=?.0?>
Start Element
<soapenv:Envelope =http://schemas.xmlsoap.org/soap/envelope/ =http://www.w3.org/2001/XMLSchema =http://www.w3.org/2001/XMLSchema-instance>
Start Element
<soapenv:Body>
Start Element
<ns1:logonResponse =http://schemas.xmlsoap.org/soap/encoding/ =G2A_Sessions>
Start Element
<connectionId =xsd:string>
98CF389DD6CA710
</connectionId>
End Element
</ns1:logonResponse>
End Element
</soapenv:Body>
End Element
</soapenv:Envelope>
End Element
END DOCUMENT
SessID 98CF389DD6CA710
Made Connection
IOException Occurred : java.io.IOException: Server returned HTTP response code: 500 for URL: [The ssl url in the program]java.io.IOException: Server returned HTTP response code: 500 for URL: [The ssl url in the program]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1153)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at MySAXExample.G2A_Query(MySAXExample.java:185)
at MySAXExample.dataComn(MySAXExample.java:239)
at MySAXExample.main(MySAXExample.java:246)
Output completed (4 sec consumed) - Normal Termination
For the first request it's working fine and is giving me a connectionId. That is the sessId i printed.
but when i prepare a soap message with that connection id to get further details of the connection, it gives me this error.
can anybody please tell me what i'm doing wrong here..
Thank you very much in advance.
my thoughts on this are, the first time, i'm sending a soap message with tags ns1:logonResponse with username and password and am getting a connection id in response. now the httpsurlconnection which i am using at this point expires.
now for the second set of data i need to use a new httpsurlconnection, but since this time i'm not sending the ns1:logonResponse tag and the username and the password, and am directly asking for the data, the server is not recognizing me.

