4
private void CreateaMultiPartRequest() {
   try{
       HttpClient client = new DefaultHttpClient();  
       HttpPost post = new HttpPost("http://172.16.22.232:8080/RESTfulExample/rest/multiPartFileUpload");  
       MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       post.setHeader("Content-type", "multipart/form-data; boundary="+"~~~~~");
       String Usersettings = "{\"uId\":\"atcc02\", \"bomId\":\"IC014654_12345\", \"fileName\":\"file_12345.pdf\"}";     
       File cfile = new File(receivedUri.getPath());
       reqEntity.addPart("file", new FileBody(cfile,"application/octet")); 
       reqEntity.addPart("settings", new StringBody(Usersettings,"application/json",Charset.forName("UTF-8")));
       post.setEntity(reqEntity); 
       HttpResponse response = client.execute(post);  
       HttpEntity resEntity = response.getEntity();  
            if (resEntity != null)  
                {
                  resEntity.consumeContent();  
                }
                else{                       
                }
            }           
            catch(Exception e)
            {
            }
        }

Whenever i am trying to execute this code, iam getting a "400:BAD REQUEST" from server. Not able to make out the issue here, Can some one help me in rectifying this issue ?

B770
  • 1,272
  • 3
  • 17
  • 34
Rahul Raj
  • 367
  • 3
  • 17
  • Did you check this http://stackoverflow.com/questions/11629013/android-error-multipartentity-request-sent-by-the-client-was-syntactically-i?rq=1 – Manish Dubey May 15 '13 at 11:58
  • Yes .. I checked the same.. In fact i have followed the same. Still the issue is seen – Rahul Raj May 15 '13 at 13:36
  • Is this code for Android? MultipartEntity is not part of Android libs. – Pointer Null Jun 13 '13 at 07:00
  • Did you check your request in wireshark? Perhaps this will help – B770 Jun 14 '13 at 09:21
  • You're sending something wrong, according to 400 response code description `The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications`. 400 usually returns in a message what is the cause. You should check that first ... – gunar Jun 17 '13 at 08:47

2 Answers2

0

Possible you use wrong MIME type for FileBody. Try application/octet-stream instead.

To get correct MIME type I use follow code:

MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

Look out my small library, that I use to make network requests: http://code.google.com/p/android-dman/source/browse/trunk/src/org/nkuznetsov/lib/dman/DownloadManager.java

Nik
  • 7,114
  • 8
  • 51
  • 75
0

Here you can try this, it works for me fine.

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);

        FileBody bin = new FileBody(new File(args[5]));
        long size = bin.getContentLength();

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("image1", bin);
        String content = "";
        try {
             httpPost.setEntity(reqEntity);
                HttpResponse response = httpClient.execute(httpPost, localContext);
                HttpEntity ent = response.getEntity();
                InputStream st = ent.getContent();
                StringWriter writer = new StringWriter();
                IOUtils.copy(st, writer);
                content = writer.toString();                

        } catch (IOException e) {                   

        }
Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40