0

我正在尝试使用 addField 方法向 Multipart HTTP POST 添加额外的字段,但是当我使用 WireShark 捕获数据包时,我看不到它的效果。可能是什么问题呢?

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);
        }
4

1 回答 1

2

好的,我找到了答案。问题是,当您创建一个新的 MultipartEntity 时,它会生成一个随机边界,但我的服务器正在等待我自己的边界。所以我只需要将 MultipartEntity 构造函数更改为:

String boundary = "---------------------------Yout own boundary";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, boundary, null);
于 2013-07-25T15:11:37.393 回答