1

I am writing SOAP client using WSDL. I have similar code in PHP, which works fine. However, I have some problems in Java. Here is my code snippet:

Where should I define the path to my local WSDL file?

Any idea is welcome.

try 
{
 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
 SOAPConnection soapConnection = soapConnectionFactory.createConnection();

 SOAPMessage soapResponse = soapConnection.call(mySampleQuery(), 
                     "https://www.webpagename.com");

 // Process the SOAP Response
 printSOAPResponse(soapResponse);

 soapConnection.close();
} 
catch (Exception e) 
{
 System.err.println("Error occurred while sending SOAP Request to Server");
 e.printStackTrace();
}

private static SOAPMessage queryFlightsByAerodrome() throws Exception
{     
 MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
 SOAPMessage soapMessage = messageFactory.createMessage();
 SOAPPart soapPart = soapMessage.getSOAPPart();

 String serverURI = "localfolder/wsdlfilename.wsdl";

 // SOAP Envelope
 SOAPEnvelope envelope = soapPart.getEnvelope();
 envelope.addNamespaceDeclaration("mydata", serverURI);

 // SOAP Body
 SOAPBody soapBody = envelope.getBody();
 SOAPElement soapBodyElem = soapBody.addChildElement("mySampleRequest", "mydata");

 SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("userId");
 soapBodyElem1.addTextNode("testuser");

 //...here I create soapBodyElem1 

 MimeHeaders headers = soapMessage.getMimeHeaders();

 headers.addHeader("SOAPAction", serverURI + "queryTestData");

 System.out.println("Content description: "+soapMessage.getContentDescription());
 soapMessage.saveChanges();

 /* Print the request message */
 System.out.print("Request SOAP Message:");
 soapMessage.writeTo(System.out);
 System.out.println();

 return soapMessage;
}

After running this code I get the following error at line SOAPMessage soapResponse = soapConnection.call(mySampleQuery(),"https://www.webpagename.com"):

Nov 03, 2014 4:18:27 PM com.sun.xml.internal.messaging.saaj.soap.MessageImpl identifyContentType SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message Error occurred while sending SOAP Request to Server com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response? at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(Unknown Source) at com.aslogic.isagent.MyAgent.main(MyAgent.java:46) Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response? at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.identifyContentType(Unknown Source) at com.sun.xml.internal.messaging.saaj.soap.MessageFactoryImpl.createMessage(Unknown Source) at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(Unknown Source)

kolossus
  • 20,559
  • 3
  • 52
  • 104
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • are you sure your service url is correct? check with browser what its returning – JIV Nov 03 '14 at 15:56
  • @JIV: As I mentioned, I have similar code in PHP and it works fine. So, it means that service url is correct. However, in PHP I define the path to WSDL file and certificate file. I don't know how to do this in Java. – Klausos Klausos Nov 03 '14 at 16:09
  • actually i don't think you building request correctly, maybe you should look on wsdl2java and try invoking client stub which is generated from wsdl, would be much easier for you – JIV Nov 03 '14 at 16:24
  • The strange thing is that System.out.println("Content description: "+soapMessage.getContentDescription()); returns NULL. May this be the problem? – Klausos Klausos Nov 03 '14 at 16:29

1 Answers1

3

Invalid Content-Type. Could be an error message instead of a SOAP message

That's your first clue right there.

Invalid Content-Type:text/html. Is this an error message instead of a SOAP response

That's another clue. Putting it all together:

  1. Your client is connecting to the service
  2. The service is returning a non-SOAP response payload (it's returning HTML instead), hence the reason why SAAJ is choking on it.

Typically, a webservice will return HTML when it's responding with a standard JavaEE container-generated error page, as against the expected SOAP response. The question now is: What is the error response that's being returned? Talk to your service provider for feedback on what you're doing wrong. Also, look into logging everything that hits your client somehow

Related

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104