Hi, I am using HttpClient 3.0.1 to send SOAP request. The HTTP request may
also contains an attachment file.
I constructed a PostMethod and set MultipartRequestEntity. I put the
SOAPRequest as a StringPart, and the attachment as a FilePart. HttpClient
does not generate the desired MIME Request. I want to specify the StringPart
content-type as "text/xml; charset=UTF-8", but HttpClient just generated
"text/plain; charset=US-ASCII" even if I explicitly set content-type header.


The following is my code:

HttpClient client = new HttpClient();
      PostMethod post = new PostMethod(serviceUrl);

      RequestEntity requestEntity;

      try {
       if(originalFileName != null && !originalFileName.trim().equals("") )
{
        // there is some attachment.
        log.debug("Multipart Request Entity.###################");

        Part[] parts = new Part[] {new StringPart("soapPart", soapRequest),
new FilePart(tempAttachment.getName(), tempAttachment)};
        post.setRequestEntity(new MultipartRequestEntity(parts,
post.getParams()));
        //post.setRequestHeader("Content-type", "multipart/form-data;
charset=UTF-8");
       } else {
        log.debug("String Request Entity.###################");
        requestEntity = new StringRequestEntity(soapRequest);
        post.setRequestEntity(requestEntity);
        //post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
       }

    post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
    post.setRequestHeader("SOAPAction", soapAction);

    int statusCode1 = client.executeMethod(post);
    log.debug("Response: " + post.getResponseBodyAsString());


The generated HTTP request is as follows:
#####################################################################################
Generated HTTP Request

#####################################################################################

--vR8W_-xJ9O3bOHUKC5gfV1hBiL4lare
Content-Disposition: form-data; name="soapPart"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/";
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/";
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";>
 <SOAP-ENV:Body xmlns="http://ejb.hwams.trade.crimsonlogic.com/xsd";>
   <submitManifest>
     <senderID>sampleSenderSenderID</senderID>
     <docType>zip</docType>
     <submissionFileName>ejb.zip</submissionFileName>
   </submitManifest>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--vR8W_-xJ9O3bOHUKC5gfV1hBiL4lare
Content-Disposition: form-data; name="ejb.zip"; filename="ejb.zip"
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

PK


#####################################################################################
Expected HTTP Request

#####################################################################################

------=_Part_44_15537607.1179138376175
Content-Type: text/xml

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/";
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"; xmlns:xsd="
http://www.w3.org/2001/XMLSchema"; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";>
 <SOAP-ENV:Body xmlns="http://ejb.hwams.trade.crimsonlogic.com/xsd";>
   <submitManifest>
     <senderID>sampleSenderSenderID</senderID>
     <docType>zip</docType>
     <submissionFileName>ejb.zip</submissionFileName>
   </submitManifest>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
------=_Part_44_15537607.1179138376175
Content-Type: application/octet-stream
Content-Id: ejb.zip

PK


Could you help me with this?


Regards,
Xinjun

Reply via email to