This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Services and the fly likes HttpClient and MultipartEntity vs. Jersey Multipart Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "HttpClient and MultipartEntity vs. Jersey Multipart " Watch "HttpClient and MultipartEntity vs. Jersey Multipart " New topic
Author

HttpClient and MultipartEntity vs. Jersey Multipart

Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Hello,

I have a rest webservice that takes a POST metod with multipart message:

Now I am trying to write a java client for that. I started with a simple jersey client:

This works great - everything is ok. However I need this client working on Android and I have trouble with using jersey on that platform. So I used the normal way to send multipart message on android:

An this gives an annoying response from the server :


I check the server log files but there is nothing there. So I don't know what's the origin of this error. I have wrote a simple html page with a post formula and 'multipart/form-data' content-type and it also works! An auto-generated request from soapUI also works! Why my client does not work? Can anybody help? (For the record - tried also without Base64encoding - no change).
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I don't know what causes the problem, but have you tried to write a servlet filter to examine the requests to your web service?
This way you can compare the requests from the Jersey client and the Android phone to see where the difference is and, hopefully, solve the problem.
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Ivan Krizsan wrote:Hi!
I don't know what causes the problem, but have you tried to write a servlet filter to examine the requests to your web service?
This way you can compare the requests from the Jersey client and the Android phone to see where the difference is and, hopefully, solve the problem.
Best wishes!

Thank you for the response. I used tcpmon to look at what is sended:

The working Jersey version:

And the not working HttpClient approach:

However that does not tell me anything about how to modify the HttpClient code to make it work.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Does the Android framework use MIME-envelopes?
The Jersey request reminds me of something I have seen in connection to SOAP with attachments, which uses MIME envelopes to wrap the SOAP request and the attached data.
Apart from that, I must admit that I am not familiar with Android client programming.
Best wishes!
Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Ivan Krizsan wrote:Hi!
Does the Android framework use MIME-envelopes?
The Jersey request reminds me of something I have seen in connection to SOAP with attachments, which uses MIME envelopes to wrap the SOAP request and the attached data.
Apart from that, I must admit that I am not familiar with Android client programming.
Best wishes!

The client which is not working and giving me this error message is also running on PC and still NOT working. It is just written in a way Android should accept. So fro now it is not even the case of android environment. If i can get this message through with HttpClient it should also work on Android. So the question is - can I use MIME envelope with HttpClient and MultipartEntity...
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Have you seen this web page: http://hc.apache.org/httpclient-3.x/methods/multipartpost.html
?
Best wishes!
Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
Ivan Krizsan wrote:Hi!
Have you seen this web page: http://hc.apache.org/httpclient-3.x/methods/multipartpost.html
?
Best wishes!

Yes I also have tried this approach though the Multipart Post class is (I think) depreciated now. But thank you for the contribution Ivan, seems that no one else is willing to help.
J Butenhoff
Greenhorn

Joined: Nov 19, 2012
Posts: 1
Was this ever resolved? Just starting down a very similar path and stumbled into this post. Did notice that the HTTP that works does declare the boundary marker, though not sure how significant that is.
Adam Kronicki
Ranch Hand

Joined: Sep 01, 2009
Posts: 68
J Butenhoff wrote:Was this ever resolved? Just starting down a very similar path and stumbled into this post. Did notice that the HTTP that works does declare the boundary marker, though not sure how significant that is.


I ended up generating the multipart request 'manually' by submitting string sings to the connection stream. However later on I succeed with the jersey library on Android so I don't have this code any more. But I think you could find some similar solution for dealing with multipart messages with URLConnection class or just read the spec like I did.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpClient and MultipartEntity vs. Jersey Multipart
 
Similar Threads
Strange problem - works on maven glassfish but not on Tomcat
Sending a jason object via Post
Post the data to Server
How to convert incoming Multipart to base64binary
cannot connect to my MySQL db using HttpClient