Вопрос

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);
        }
Это было полезно?

Решение

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top