I have an application that sends SOAP messages using the SOAP API.
It worked from office perfectly, but when on remote from home using VPN, SOAP calls don't work anymore.
Error message is :
[Fatal Error] :1:1: Content is not allowed in prolog.
Content is not allowed in prolog.
The code I use for soap calls :
SOAPConnection con;
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
con = conFactory.createConnection();
URL endpoint = new URL(this.getURI());
SOAPMessage retval = con.call(message, endpoint);
How to add a proxy to make those calls work when using a VPN?
Related
I'm currently working on a custom SOAP call to a specific domain beyond my control. I know the SOAP call fails but I cannot seem to grab the returned (wrong)value.
Right now I'm using the code below:
Document document = convertStringToDocument(this.MeldingString);
// System.out.println(document);
SOAPConnectionFactory myFct = SOAPConnectionFactory.newInstance();
MessageFactory myMsgFct = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = myMsgFct.createMessage();
SOAPConnection myCon = myFct.createConnection();
// Adding parts
SOAPPart mySPart = message.getSOAPPart();
SOAPEnvelope myEnvp = mySPart.getEnvelope();
// Escape the password for usage in header
String escpwd = StringEscapeUtils.escapeJava(this.Password);
// Header
MimeHeaders headers = message.getMimeHeaders();
headers.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
headers.setHeader("Authorization", this.Username + ":" + escpwd);
// Body
SOAPBody body = myEnvp.getBody();
body.addDocument(document);
// Sending
Core.getLogger("GetResultSOAPmsg").trace("Started");
URL endPt = new URL(
"URL-TO-MY-SERVICE");
System.setProperty("java.net.useSystemProxies", "true");
try {
SOAPMessage reply = myCon.call(message, endPt); "UTF-8");
}
catch(Exception e)
{
e.printStackTrace();
}
This yields the following error which is very common al throughout SO:
SEVERE: SAAJ0537: Invalid Content-Type. Could be an error message instead of a SOAP message
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?
Now I have read most of these topics already and they explain how this problem is usually solved (namespaces, escaping URL, etc.) but I cannot seem to figure out what is wrong in my case. This is a private service and the other side is unfortunately unable to assist me in this case. The error could be anything from wrong certificates to misspelling the URL.
Therefore I would like to actually SEE onscreen what the actual reply is that was received when making the call. This is going to help me (assuming it's something like a 503, 404 or other page). Regardless of what I do and where I set my breakpoints, there is no information on Reply. It makes somewhat sense since it was unable to create said object but the entire message seems to be discarded.
In what way will I be able to see what the actual reply was to my call before it is discarded?
I think there's a problem with your header
headers.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
try something like
headers.setHeader("Content-Type", "application/xml;charset=UTF-8");
or
headers.setHeader("Content-Type", "application/json;charset=UTF-8");
depending on the content type that the content type accepted by the service
I am trying to access SAOP API using SAAJ.
I have followed the same code that were present in other answers on creating a SOAP request body and
calling the SOAP API.
This particular service does not need any authentication (as mentioned in API doc) nor does it take any input parameters.
From WSDL: (copied only relevant content)
<xs:complexType name="getCustomerNames">
<xs:sequence/>
</xs:complexType>
...
<wsdl:operation name="getCustomerNames">
<wsdl:input message="tns:getCustomerNames" name="getCustomerNames"/>
<wsdl:output message="tns:getCustomerNamesResponse" name="getCustomerNamesResponse"/>
<wsdl:fault message="tns:SOAPException" name="SOAPException"/>
</wsdl:operation>
Below is the Java code that creates the soap body and calls the API
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = scf.createConnection();
SOAPFactory sf = SOAPFactory.newInstance();
String serverURI = "com.webservices.services.authentication";
MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL); // I tried without parameters as well
SOAPMessage message = mf.createMessage();
message.getSOAPHeader().detachNode();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("auth", serverURI);
Name bodyName = sf.createName("getCustomerNames", "auth", serverURI);
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
URL endpoint = new URL(urlendpoint); // urlendpoint is the one that I took from WSDL file service endpoint.
SOAPMessage response = connection.call(message, endpoint);
connection.close();
I get the below error message whenever I execute this. I could not understand why.. The same url is returning the
proper response when I execute curl command from Unix and post the same request xml that was created by above java code.
Feb 14, 2017 3:21:54 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
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid Content-Type:text/html. Is this an error message instead of a SOAP response?
That error means that the server responds with an HTML page instead of a SOAP response. To debug this, you need to intercept the response and look at the content of the HTML page; it will probably contain more information about the problem.
I am using the below code to send SOAP request to an endpoint. Since we are experiencing some delay in getting the response from server,i would like to set connection timeout in the code.
Code:
SOAPConnectionFactory soapConnectionFactory =SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
MimeHeaders header = new MimeHeaders();
header.addHeader("Content-type", "application/soap+xml;charset=UTF-8");
header.addHeader("SOAPAction", soapAction);
InputStream is = new ByteArrayInputStream(reqXML.getBytes());
SOAPMessage request = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(header, is);
SOAPMessage soapResponse = soapConnection.call(request, endPoint);
Can some one help me to modify my code to add the timeout value?
I have gone through thread Setting socket read timeout with javax.xml.soap.SOAPConnection ralted to same issue.I am unable to use the solution provided here as I get failure if I do not specify the soap version while creating soap message.
Since, SOAP uses jave.net.URLConnection underneath, you can set the system parameters for connection timeout with
is going to be valid for SOAP as well. You can set the following parameters
-Dsun.net.client.defaultConnectTimeout= < timeout>
-Dsun.rmi.transport.proxy.connectTimeout= < timeout>
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:
Your client is connecting to the service
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
Tracing XML request/responses with JAX-WS when error occurs
I’m trying to use the following SOAPClient for Java (details which I’ve obtained from the following tutorial http://users.skynet.be/pascalbotte/rcx-ws-doc/saajpost.htm).
However, it seems to be throwing an exception.
Here is my code:
javax.xml.soap.SOAPMessage msg;
MessageFactory mf = MessageFactory.newInstance();
msg = mf.createMessage();
SOAPPart part = msg.getSOAPPart();
StreamSource source = new StreamSource(new File("samples/input1.xml”));
part.setContent(source);
msg.saveChanges();
String endpoint = "http://ws1.parasoft.com/glue/calculator";
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();
javax.xml.soap.SOAPMessage response = conn.call(msg, endpoint);
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
Source sc = response.getSOAPPart().getContent();
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
StreamResult result = new StreamResult(ostream);
tf.transform(sc, result);
conn.close();
System.out.println(new String(ostream.toByteArray(), "UTF-8”));
In this example, we assume samples/input1.xml holds the following:
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<s11:Body>
<ns1:add xmlns:ns1="http://www.parasoft.com/wsdl/calculator/">
<ns1:x>248</ns1:x>
<ns1:y>365</ns1:y>
</ns1:add>
</s11:Body>
</s11:Envelope>
The sample web-service that I’m trying to use can be found here:
http://www.service-repository.com/client/operations
When running the above Java-code (using the SOAPClient library), the following exception is thrown:
Jul 19, 2012 3:50:11 AM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; cannot find /calculator
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:148)
at corebus.test.deprecated.TestMain.main(TestMain.java:1870)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
... 1 more
CAUSE:
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (404cannot find /calculator
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:258)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:144)
Using this web-site, it seems the services is up and running, and working well.
I have even verified the service-endpoint is active, by conducted a simple cURL request, which surprisingly produces the correct output.
curl -H "Content-Type: text/xml; charset=utf-8" -H "SOAPAction:http://www.parasoft.com/wsdl/calculator/" -d#soap-request.xml http://ws1.parasoft.com/glue/calculator
The output produced is:
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/‘>
<soap:Body>
<n:addResponse xmlns:n='http://www.parasoft.com/wsdl/calculator/‘>
<n:Result xsi:type='xsd:float'>613.0</n:Result>
</n:addResponse>
</soap:Body>
</soap:Envelope>
So, my question is: Firstly, what is wrong with the Java-code? And how could it be fixed? Or also, is there any other better/more-reliable Generic SOAPClient Library that would be recommended?
Have a look at the WSDL by attaching ?wsdl in your url in browser or somehow get he WSDL. Check if the WSDl specifies if it is a document style or PRC style and based on that prepare the input xml.
If possible use SOAP UI tool which can be very handy.