简体   繁体   中英

How to set up SOAPMessage boundary?

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM