SOAP Message Factory response error
I am writing both the server and the client. The server builds the XML using JAXB marshalling and piping back to the client in the HTTP socket. From the client the request is built using the message factory and sent over the connection factory instantiation. When the response is received the SAAJ API indicates the response was bad and equals null. The client is a applet.
Error Message
Code :
Jun 29, 2008 5:01:49 PM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; null
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (-1null
Client Code
Code :
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
if(connection == null)
connection = soapConnectionFactory.createConnection();
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();
QName bodymain = new QName("icb_xml");
SOAPBodyElement mainbodyElement = body.addBodyElement(bodymain);
...
...
...
URL endpoint = new URL(serverurl);
System.out.println("b");
SOAPMessage response = connection.call(message, endpoint);
Comm_log
Code :
[Client Request]>POST / HTTP/1.1
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
Content-Length: 242
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java(tm) 2 SDK, Standard Edition v1.6.0_05 Java/1.6.0_05
Host: sjdpprojectserver.hopto.org
Connection: keep-alive
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><icb_xml><login><username>jaco</username><password>12345</password><language>English</language></login></icb_xml></SOAP-ENV:Body></SOAP-ENV:Envelope>
[Server Response]><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><icb_xml><loginResponse><err_code>0</err_code><user_id>101</user_id></loginResponse></icb_xml></env:Body></env:Envelope>
In your opinion is this a server or client related problem or should I look for an alternative function rather than the SOAP message factory and connection factory?
Regards,
Buglish
Re: SOAP Message Factory response error
After I had placed the post I noticed my response SOAP message format is not the same is the client. I used in both instences the MessageFactory.
On the server side how do I detect the SOAP version?
I had change the instantiation from
Code :
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
to
Code :
messageFactory = MessageFactory.newInstance();
on the server.
Code :
[Client Request]>POST / HTTP/1.1
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
Content-Length: 241
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java(tm) 2 SDK, Standard Edition v1.6.0_05 Java/1.6.0_05
Host: sjdpprojectserver.hopto.org
Connection: keep-alive
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><icb_xml><login><username>rsdth</username><password>sth</password><language>English</language></login></icb_xml></SOAP-ENV:Body></SOAP-ENV:Envelope>
[Server Response]><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><icb_xml><loginResponse><err_code>102</err_code><err_data>Login Failed. Incorrect username or password.</err_data></loginResponse></icb_xml></SOAP-ENV:Body></SOAP-ENV:Envelope>
Unfortunatly this did not resolve the problem as I still receive the above error message
Re: SOAP Message Factory response error
Hi,
To solve this problem; I used the SOAPMEssageFactory to only create the SOAP packet and HTTPConnection for the transmission protocol. It's a normal socket push which is allot simpler and old school. This did not give my any environment errors.
Code :
public String sendSoapLoginMessage(String usernameintput, String passwordintput) throws Exception
{
SOAPFactory soapFactory = SOAPFactory.newInstance();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage message = mf.createMessage();
SOAPBody body = message.getSOAPBody();
// You can detach the SOAP header in this example
// msg.getSOAPHeader().detachNode();
Name bodyName = soapFactory.createName("icb_xml" , "sjp", serverurl);
SOAPElement xmlElement = body.addBodyElement(bodyName);
Name loginName = soapFactory.createName("login");
SOAPElement loginElement = xmlElement.addChildElement(loginName);
SOAPElement child = soapFactory.createElement("username");
child.addTextNode(usernameintput);
loginElement.addChildElement(child);
child = soapFactory.createElement("password");
child.addTextNode(passwordintput);
loginElement.addChildElement(child);
child = soapFactory.createElement("language");
child.addTextNode(icb.language);
loginElement.addChildElement(child);
message.saveChanges();
// Send the message
System.out.println("Send the SOAP message\n");
String server = serverurl;
String response = "";
try
{
if(out==null)
{
if(uc == null)
{
u = new URL(DEFAULT_SERVER);
uc = u.openConnection();
}
if(urlconnection == null)
{
urlconnection = (HttpURLConnection) uc;
urlconnection.setDoOutput(true);
urlconnection.setDoInput(true);
urlconnection.setRequestMethod("POST");
urlconnection.setRequestProperty("SOAPAction", SOAP_ACTION);
}
out = urlconnection.getOutputStream();
}
if(wout == null)
wout = new OutputStreamWriter(out);
String soapmessage = printMessage(message);
wout.write(soapmessage);
wout.flush();
//wout.close();
System.out.println(2);
if(is == null)
is = urlconnection.getInputStream();
response = readServerResponseMessage(is);
//is.close();
int sizemore = "</loginResponse>".length();
response = response.substring(response.indexOf("<loginResponse>"),response.indexOf("</loginResponse>")+sizemore);
System.out.println("[SERVER RESPONSE]"+response);
}
catch (IOException e)
{
System.err.println(e);
}
catch(Exception eb)
{
System.err.println(eb);
}
return response;
}
public void closeConnections()
{
try
{
wout.close();
is.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String readServerResponseMessage(InputStream is)
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
char[] readChars = new char[65000];
String response = "";
try
{
while (br.read(readChars) != -1)
{
response = new String(readChars);
break;
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return response;
}
Regards,
Buglish
Re: SOAP Message Factory response error
Thank you for posting your solution Buglish.
This seems to be a very popular thread so no doubt it will help others.