title: HttpClient and MultipartEntity vs. Jersey Multipart and Android
body: <p>I have a rest webservice that takes a POST metod with multipart message:</p>
  
  <pre><code>@Path("transferFile")     
   @POST  
  @Consumes(MediaType.MULTIPART_FORM_DATA)  
  @Produces(MediaType.APPLICATION_XML)  
  public String multipartTest(com.sun.jersey.multipart.MultiPart data) {  
  try {  
  // get first body part (index 0)          
  BodyPart bp = multiPart.getBodyParts().get(0);  
  etc..  
  </code></pre>
  
  <p>Now I am trying to write a java client for that. I started with a simple jersey client:
  view plaincopy to clipboardprint?</p>
  
  <pre><code>    MultiPart multiPart = new MultiPart();  
      multiPart.bodyPart( new BodyPart(wavestream,MediaType.APPLICATION_OCTET_STREAM_TYPE));  
  
      Client c = Client.create();  
      WebResource r = c.resource("http://127.0.0.1:8080/webapp:);
  response=r.path("transferFile").type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_XML).post(String.class, multiPart);  
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    HttpClient client = new DefaultHttpClient();
   client.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second 
  
  HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/webapp/transferFile");
   httpPost.setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA );
  
  //tried with and without base64
   byte [] encodedWavestream = Base64.encodeBytesToBytes(wavestream);
   InputStream ins = new ByteArrayInputStream(encodedWavestream);
   InputStreamBody body = new InputStreamBody(ins, "test" );
   int send = ins.available(); 
  
  MultipartEntity requestContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE ); 
  requestContent.addPart("stream", body);
  
  httpPost.setEntity(requestContent); 
  HttpResponse Response = client.execute(httpPost);
  </code></pre>
  
  <p>An this gives an annoying response from the server :</p>
  
  <pre><code>HTTP Status 400 - Bad Request  
  The request sent by the client was syntactically incorrect (Bad Request).   
  </code></pre>
  
  <p>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?</p>