You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Xinjun Chen <xj...@gmail.com> on 2007/05/14 12:39:21 UTC
[HttpClient 3.0.1] cannot set Content-Type when using MultipartRequestEntity
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
Re: [HttpClient 3.0.1] cannot set Content-Type when using
MultipartRequestEntity
Posted by Roland Weber <RO...@de.ibm.com>.
Hi Xinjun,
> You mentioned that questions regarding sending SOAP with attachment
appear
> in the maillist before, but I really cannot find it. I only find one
thread
> regarding PostSOAP. Could you kindly give a link to the thread?
I have a deja vu. Is there an error in the matrix?
This is the one I remember...
http://mail-archives.apache.org/mod_mbox/jakarta-httpclient-user/200608.mbox/%3c84f31d220608232303wa8321c3yd5296ad0f97a7917@mail.gmail.com%3e
> By the way, is it necessary to send SOAP as a file in HttpClient? I feel
> sending SOAP as string part should also be OK. Please advice.
You can implement your SOAP attachments any way you want.
HttpClient doesn't care. I don't know what SOAP requires.
cheers,
Roland
Re: [HttpClient 3.0.1] cannot set Content-Type when using MultipartRequestEntity
Posted by Xinjun Chen <xj...@gmail.com>.
Hi Roland,
You mentioned that questions regarding sending SOAP with attachment appear
in the maillist before, but I really cannot find it. I only find one thread
regarding PostSOAP. Could you kindly give a link to the thread?
By the way, is it necessary to send SOAP as a file in HttpClient? I feel
sending SOAP as string part should also be OK. Please advice.
Regards,
Xinjun
On 5/15/07, Xinjun Chen <xj...@gmail.com> wrote:
>
> Sure. I'd like to add some documentation for the SOAP with attachment
> using HttpClient after I successfully test all the codes.
>
> Regards,
> Xinjun
>
>
> On 5/15/07, Roland Weber <RO...@de.ibm.com> wrote:
> >
> > Hello Xinjun
> >
> > > Do you mean I should create another RequestEntity class to extend the
> > > current MultipartRequestEntity and override the getContentType()
> > method?
> >
> > Yes.
> >
> > > From the getContentType() method, we can find that the contentType is
> > fixed
> > > to be "multipart/form-data". What if I setContentType to the post
> > method
> > > explicitly? Will it take any effect?
> >
> > There is no setContentType in PostMethod.
> >
> > > 2. Http contentType:
> > > Before executing post method, I set the content type as follows:
> >
> > I have written in my previous mail that this will not take effect.
> >
> >
> > > But the effect is that the Http body is lost inspecting from
> > TCPMonitor.
> >
> > I suspect that you made some other mistake to loose the entity.
> >
> > > By the way, I searched the HttpClient documentations and samples from
> >
> > > Apache website, but could not find information about sending SOAP with
> > > attachment files. I think this task should be quite common. If
> > possible,
> > > could you give me some pointer to that?
> >
> > Similar questions have appeared before, please search the
> > mailing list archives. This project, including it's documentation,
> > is a community effort. If you've got the time, please compile
> > your findings in a Wiki page. Then we can point to that in
> > the future.
> >
> > http://wiki.apache.org/jakarta-httpclient/
> >
> > hope that helps,
> > Roland
> >
> >
> >
>
Re: [HttpClient 3.0.1] cannot set Content-Type when using MultipartRequestEntity
Posted by Xinjun Chen <xj...@gmail.com>.
Sure. I'd like to add some documentation for the SOAP with attachment using
HttpClient after I successfully test all the codes.
Regards,
Xinjun
On 5/15/07, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Xinjun
>
> > Do you mean I should create another RequestEntity class to extend the
> > current MultipartRequestEntity and override the getContentType() method?
>
> Yes.
>
> > From the getContentType() method, we can find that the contentType is
> fixed
> > to be "multipart/form-data". What if I setContentType to the post method
> > explicitly? Will it take any effect?
>
> There is no setContentType in PostMethod.
>
> > 2. Http contentType:
> > Before executing post method, I set the content type as follows:
>
> I have written in my previous mail that this will not take effect.
>
>
> > But the effect is that the Http body is lost inspecting from TCPMonitor.
>
> I suspect that you made some other mistake to loose the entity.
>
> > By the way, I searched the HttpClient documentations and samples from
> > Apache website, but could not find information about sending SOAP with
> > attachment files. I think this task should be quite common. If possible,
> > could you give me some pointer to that?
>
> Similar questions have appeared before, please search the
> mailing list archives. This project, including it's documentation,
> is a community effort. If you've got the time, please compile
> your findings in a Wiki page. Then we can point to that in
> the future.
>
> http://wiki.apache.org/jakarta-httpclient/
>
> hope that helps,
> Roland
>
>
>
Re: [HttpClient 3.0.1] cannot set Content-Type when using
MultipartRequestEntity
Posted by Roland Weber <RO...@de.ibm.com>.
Hello Xinjun
> Do you mean I should create another RequestEntity class to extend the
> current MultipartRequestEntity and override the getContentType() method?
Yes.
> From the getContentType() method, we can find that the contentType is
fixed
> to be "multipart/form-data". What if I setContentType to the post method
> explicitly? Will it take any effect?
There is no setContentType in PostMethod.
> 2. Http contentType:
> Before executing post method, I set the content type as follows:
I have written in my previous mail that this will not take effect.
> But the effect is that the Http body is lost inspecting from TCPMonitor.
I suspect that you made some other mistake to loose the entity.
> By the way, I searched the HttpClient documentations and samples from
> Apache website, but could not find information about sending SOAP with
> attachment files. I think this task should be quite common. If possible,
> could you give me some pointer to that?
Similar questions have appeared before, please search the
mailing list archives. This project, including it's documentation,
is a community effort. If you've got the time, please compile
your findings in a Wiki page. Then we can point to that in
the future.
http://wiki.apache.org/jakarta-httpclient/
hope that helps,
Roland
Re: [HttpClient 3.0.1] cannot set Content-Type when using MultipartRequestEntity
Posted by Xinjun Chen <xj...@gmail.com>.
Hi Roland,
Do you mean I should create another RequestEntity class to extend the
current MultipartRequestEntity and override the getContentType() method?
>From the getContentType() method, we can find that the contentType is fixed
to be "multipart/form-data". What if I setContentType to the post method
explicitly? Will it take any effect?
I have written some code for this task. My codes look ok but just doesn't
work. I have the following specific questions regarding using HttpClient to
achieve the purpose of sending SOAP with attachment files:
1. SOAP part:
StringPart soapPart = new StringPart("soapPart", soapRequest);
soapPart.setContentType("text/xml");
soapPart.setCharSet(null);
What kind of CharSet should I use? When I use XML Exchanger to send an HTTP
request with SOAP and attachment files, the charset is not set in soapPart
or filePart. However, HttpClient by default add ISO-8859-1 as charset. Is it
compulsary for me to setCharSet as null?
2. Http contentType:
Before executing post method, I set the content type as follows:
post.setRequestHeader("Content-Type", "multipart/related; boundary=" +
soapPart.getBoundary());
But the effect is that the Http body is lost inspecting from TCPMonitor.
By the way, I searched the HttpClient documentations and samples from
Apache website, but could not find information about sending SOAP with
attachment files. I think this task should be quite common. If possible,
could you give me some pointer to that?
Regards,
Xinjun
On 5/14/07, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Xinjun,
>
> > 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.
>
> You can override MultipartRequestEntity.getContentType() to specify
> the value for the Content-Type header. An explicitly set header will
> be overwritten.
>
> hope that helps,
> Roland
>
>
Re: [HttpClient 3.0.1] cannot set Content-Type when using
MultipartRequestEntity
Posted by Roland Weber <RO...@de.ibm.com>.
Hello Xinjun,
> 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.
You can override MultipartRequestEntity.getContentType() to specify
the value for the Content-Type header. An explicitly set header will
be overwritten.
hope that helps,
Roland