0

I am trying to add extra field to Multipart HTTP POST with addField method, but when I catch the packet with WireShark, I cannot see the effect of it. What could be the problem?

private void upload3(File file) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url + "?recname=" + fileName);
        MultipartEntity entity = new MultipartEntity();

        String boundary = "---------------------------This is the boundary";

        httpPost.addHeader("Content-Type", "multipart/form-data; boundary="
                + boundary);


        try {
            File f = new File(  file_path);

            FileBody body = new FileBody(f);
            FormBodyPart fbp = new FormBodyPart( "file", body );
            fbp.addField("Content-Type", "audio/mp4");

            entity.addPart(fbp);


        } catch (Exception e) {
            e.printStackTrace();
        }

        httpPost.setEntity(entity);
        }
czadam
  • 1,827
  • 1
  • 18
  • 31
  • `I cannot see the effect of it` effect of what ? `MultipartEntity` is modifying Content-Type header by itself ... – Selvin Jul 25 '13 at 14:02
  • content type was just an example, if I try to set any extra fiel like filename or content transfer encoding. I cannot see the fileds in the post message. – czadam Jul 25 '13 at 14:11

1 Answers1

2

Okay, I found the answer. The problem was, when you create a new MultipartEntity it generates a random boundary, but my server was waiting for my own boundary. So I just had to change the MultipartEntity constructor to this:

String boundary = "---------------------------Yout own boundary";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, boundary, null);
czadam
  • 1,827
  • 1
  • 18
  • 31