I am sending a SoapMessage in java. I keep having problem with setting up proper headers, precisely the part of Content-Type which should looks like this:
Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart#soapui.org>"; start-info="text/xml"; boundary="the boundary of my soapmessage"
I don't know how to add the boundary parameter to my headers so it matches the rest of soap message.
My soap message looks like this:
------=_Part_0_5841104.1608651610791
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart#soapui.org>
MYXML
------=_Part_0_5841104.1608651610791
Content-Type: application/zip; name=Worker.zip
Content-Transfer-Encoding: binary
Content-ID: <MYFILE.zip>
Content-Disposition: attachment; name="MYFILE.zip"; filename="MYFILEr.zip"
MYATTACHMENT
The boundary part in soapbody is generated automatically (------=_Part_0_5841104.1608651610791) and it changes every time I send the message. When I am trying to add headers I do it this way:
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String authString = "login" + ":" + "password";
String enc = java.util.Base64.getEncoder().encodeToString((authString).getBytes(StandardCharsets.UTF_8));
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("POST","where");
hd.addHeader("Accept-Encoding","gzip,deflate");
hd.addHeader("Content-Type","multipart/related; type=\"application/xop+xml\"; start=\"<rootpart#soapui.org>\"; start-info=\"text/xml\"; boundary=\"----=_????????\"");
hd.addHeader("SOAPAction","Action");
hd.addHeader("MIME-Version","1.0");
hd.addHeader("Host","MyHost");
hd.addHeader("Connection","Keep-Alive");
hd.addHeader("User-Agent","Apache-HttpClient/4.1.1 (java 1.5)");
hd.addHeader("Authorization", "Basic " + enc);
How can I add this proper boundary to my headers?
I have struggled with this today and this was the first hit I found.
When you are done creating your SOAPMessage call saveCanges() on it. This will populate the mimeheaders with correct content-type with the bountary in it.
SOAPMessage msg = //create message...
msg.saveChanges();
msg.getMimeHeaders(); //Now contains a content-type with boundary that you can send as request properties.
Hope this helps anyone having this problem.
Related
I am looking for an advice here with my problem. I have a SOAP Request in Java with the following code:
private static SOAPMessage createSOAPRequest(String shellType, String filterCondition,
Map<String, String> AndySiteToSync) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage, shellType, filterCondition, AndySiteToSync);
MimeHeaders headers = soapMessage.getMimeHeaders();
soapMessage.saveChanges();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
soapMessage.writeTo(stream);
return soapMessage;
}
I have the following structure on the XML.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:gen="http://general.service.webservices.skire.com">
<soap:Header/>
<soap:Body>
<gen:updateShell>
<!--Optional:-->
<gen:shortname>xxx</gen:shortname>
<!--Optional:-->
<gen:authcode>xxx</gen:authcode>
<!--Optional:-->
<gen:shellType>xxx</gen:shellType>
<!--Optional:-->
<gen:shellXML>
<![CDATA[
<List_Wrapper>
<_shell>
<paraminside>letterwithutf8_ññ</uuu_longitude>
</_shell>
</List_Wrapper>
]]>
</gen:shellXML>
</gen:updateShell>
</soap:Body>
</soap:Envelope>
It is working very good with characters without ñ or áéíóú, but when I pass a parameter with ñ for example I see on the result ñ. When I print the input xml it is showing the XML fine with the correct characters but when I send the request the result is showing the incorrect character (ñ)
I paste the same input XML in the SOAPUI and the result was good, the ñ characters was introduce fine. The problem is when I send the request using Java.
I assumed it is the encoding of the message. So I added these lines before saveChanges:
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("Content-Type", "application/soap+xml;charset=UTF-8");
Alse test with:
headers.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
These headers are the same as the SOAPUI, with the exception of the "action"
I made sure the headers were ok printing it.
for (int iii = 0; iii < headers.getHeader("Content-Type").length; iii++) {
System.out.println((headers.getHeader("Content-Type"))[iii]);
}
and I got the response application/soap+xml;charset=UTF-8
This way I am creating the connection:
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(....))
SOAPUI
POST https://xxxxxxxxxxxxxxxxx/WebServices HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/soap+xml;charset=UTF-8;action="urn:xxxxxxx"
Content-Length: 4381
Host: xxxxxxxxxxxxxxxx
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Thanks.
===========================================
Thanks to Andreas I did some more testing and the encoding problem was before the SOAP Request, I got the information from a URL in json format with the following code:
JSONArray jsonDataFinal = new JSONArray();
JSONArray jsonData = (JSONArray) parser.parse(readUrl(URL));
And then:
List<Map<String,String>> mergeArray = new ArrayList<Map<String,String>>();
Map<String, String> mergeObj = null;
for (Object o : sitesArray)
{
if((siteAndy.get("xxxx")) == null) mergeObj.put("xxxx", "");
else mergeObj.put("xxxx", ((String) siteAndy.get("xxxx")).trim());
}
At what point should I use the utf-8 encoding?
Thanks
I change to this:
reader = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
And my problem was fix.
Thanks!!
I want to connect to a webservice (WS). However, a cookie must be provided in order to interact with this webservice.
So far, here is what I have:
String requiredCookieName = "requiredCookieName";
String requiredCookieValue = getRequiredCookieValue();
// Prepare SOAP message
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
soapMessage.getMimeHeaders().addHeader("SOAPAction", getSoapAction());
soapMessage.saveChanges();
// Send SOAP message
SOAPConnection soapConnection = buildSoapConnection();
SOAPBody soapBody = soapConnection
// How to add required cookie here before calling WS?
.call(soapMessage, getOperationLocation("operationName"))
.getSOAPBody();
// Process response...
How can I add the required cookie to the underlying HTTP request to WS?
You can do that by adding the corresponding Cookie HTTP header to the message (exactly as you are already doing for the SOAPAction header):
soapMessage.getMimeHeaders().addHeader(
"Cookie", requiredCookieName + "=" + requiredCookieValue);
I'm attempting to make a SOAP request to an end point programmatically through Java. I'm relatively new to Java and web services so I'm not sure what I'm doing wrong here.
Also I print out the SOAP message and can paste that into a tool like postman and enter the end point and a post is successful. So i think something with my request is not correct here.
Here is my code:
System.out.println("hey now!!!!");
try {
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection connection = scf.createConnection();
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage message = mf.createMessage();
SOAPBody body = message.getSOAPBody();
SOAPHeader header = message.getSOAPHeader();
SOAPElement getOpenPOs = body.addChildElement("GetOpenPOs", "", "https://www.autocrib.net");
SOAPElement U = getOpenPOs.addChildElement("U");
U.addTextNode("u");
SOAPElement P = getOpenPOs.addChildElement("P");
P.addTextNode("p");
SOAPElement N = getOpenPOs.addChildElement("N");
N.addTextNode("n");
SOAPElement Processed = getOpenPOs.addChildElement("Processed");
Processed.addTextNode("false");
SOAPElement StationEnd = getOpenPOs.addChildElement("StationEnd");
StationEnd.addTextNode("");
SOAPPart sp = message.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
//MimeHeaders headers = message.getMimeHeaders();
//header.setHeader("Content-Type", "text/xml");
//message.getMimeHeaders().addHeader("SOAPAction", "GetOpenPOs");
message.getMimeHeaders().addHeader("Content-Type", "text/xml");
header.setAttribute("Content-Type", "text/xml");
message.saveChanges();
System.out.println("Envelope Body");
message.writeTo(System.out);
System.out.println();
SOAPMessage reply = connection.call(message,
"https://www24.autocrib.net/WebServices/AutoCribWS.asmx");
//String reply2 = connection.call(message, "https://www24.autocrib.net/WebServices/AutoCribWS.asmx").toString();
//sp = reply.getSOAPPart();
//envelope = sp.getEnvelope();
//body = envelope.getBody();
//System.out.println(body.toString());
System.out.println("Done!!!!!!!!!!!!!!!!!!!");
} catch (Throwable t) {
System.out.println("Something went wrong!!! " + t.toString());
}
}
I get this error when I run this code:
Oct 24, 2016 1:56:57 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
Something went wrong!!! 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?
I'm guessing I need to add the Content-Type header. Am I doing this incorrectly? Any guidance would be great.
Thanks,
Tim
com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Invalid
Content-Type:text/html.
The SAAJ API throws an exception because it considers that you web service returns as response text/html content instead of soap/xml content.
So, one advise : study the content returned by postman. Are you sure it is soap/xml format ? It you notice that is not soap/xml content, work on the implementation of your WS and if needed adapt the return to be compliant with the SOAP norm.
Wilco I would like to give you credit for the answer but I don't think I can do that for comments. Your tip helped me figure out that it was indeed returning text/html because of the user agent header I had.
THanks again!!
I've the following code
String url = "http://e2e-soaservices:44000/3.1/StandardDocumentService?wsdl";
//createSOAPRequest();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
in the createSOAPRequest Method:-
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
File fXmlFile = new File("src/XML/gen_VDD7S0PLYPAS058_1409900400000_2.xml");
String xmlStr=finalXmlString(fXmlFile);
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc=docBuilder.parse(xmlStr);
System.out.println("dasdasdasd"+doc.toString());
String serverURI = "http://www.aaancnuie.com/DCS/2012/01/DocumentCreation/IStandardDocumentService/CreateDocuments";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("example", serverURI);
SOAPBody soapBody = envelope.getBody();
soapBody.setTextContent(xmlStr);
soapMessage.saveChanges();
return soapMessage;
The error message
Error occurred while sending SOAP Request to Server
java.net.MalformedURLException: no protocol: http%3A%2F%2Fe2e-soaservices%3A44000%2F3.1%2FStandardDocumentService%3Fwsdl
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at SOAPCLIENTSAAJ.main(SOAPCLIENTSAAJ.java:36)
You need to encode your URL.
Special character which have to be escaped.
Escaping example:
String url = "http://e2e-soaservices:44000/3.1/StandardDocumentService?wsdl";
String yourURLStr = java.net.URLEncoder.encode(url, "UTF-8");
java.net.URL url = new java.net.URL(yourURLStr);
So, according to the stacktrace that you posted, the actual problem is that you are trying to use a URL where various characters have been %-escaped when they shouldn't be.
http%3A%2F%2Fe2e-soaservices%3A44000%2F3.1%2FStandardDocumentService%3Fwsdl
In particular, the initial http%3A%2F%2F should actually be http://. The URL parser looks for initial : ... and when it can't find it, it complains (correctly!) that there is no protocol component to the URL.
I note that the error message does not match the URL in your code. I don't know what is going on there .... but if the URL in the error message is what is actually being used, then it needs to be decoded (not encoded).
If this is not making sense, please provide a proper complete minimal reproducible example, AND the stack trace that you get from executing that example.
I am creating a Java client for a SOAP service that takes an attachment. I'm using java.xml.soap classes, which I have uses before, but not with attachments. The server claims that my attachment is not included.
I used SoapUI, which works, and wireshark to compare my SOAP message to a working SOAP message. One big difference is that my header does not include "start=".
The working Content-Type looks like this:
Content-Type: multipart/related; type="text/xml"; start=""; boundary="----=_Part_23_6341950.1286312374228"
The Content-Type I get from my Java code is like this:
Content-Type: multipart/related; type="text/xml"; boundary="----=_Part_23_6341950.1286312374228"
No start= even when the content ID is set on the root element. The working and failing SOAP messages are otherwise nearly identical. How can I get the start tag generated, or what are other reasons the server might not see the attachment?
Thanks
SOAPMessage soapMessage =
MessageFactory.newInstance().createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody body = soapEnvelope.getBody();
SOAPHeader header = soapMessage.getSOAPHeader();
soapPart.setContentId("<rootpart#here.com>");
MimeHeaders mimeHeaders = soapMessage.getMimeHeaders();
mimeHeaders.addHeader("SOAPAction", "addDocument");
mimeHeaders.addHeader("Accept-Encoding", "gzip,deflate");
Name bodyName = soapEnvelope.createName("Document", "doc",
"http://ns/Document");
SOAPBodyElement document = body.addBodyElement(bodyName);
Name filenameName = soapEnvelope.createName("Filename", "doc",
"http://ns/Document");
SOAPElement filename = document.addChildElement(filenameName);
filename.setValue("filename.txt");
AttachmentPart attachment = soapMessage.createAttachmentPart();
attachment.setContent("Some text", "application/octet-stream");
attachment.setMimeHeader("Content-Transfer-Encoding", "binary");
soapMessage.addAttachmentPart(attachment);
SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = scf.createConnection();
URL url = new URL("http://host/Service");
SOAPMessage reply = soapConnection.call(soapMessage, url);
This works for me:
soapMessage.getMimeHeaders().setHeader("Content-Type",
soapMessage.getMimeHeaders().getHeader("Content-Type")[0]+
"; start=\"<rootpart#here.com>\"");