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)
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.