0

I am trying to post some data to a server but not getting results as expected. I get 200 OK response but the returned html source has a string saying "Error - 404 Page not found"

I think I'm doing something wrong with the set of data that I'm sending. Maybe I'm missing something as I never worked with multiform data before.

Here is the multiform data that is sent over(I've used tamper data to check what is sent over

    POSTDATA =-----------------------------124853047628807
Content-Disposition: form-data; name="mgnlModelExecutionUUID"

4ee01e05-dc16-4535-a222-693b98ec9b69
-----------------------------124853047628807
Content-Disposition: form-data; name="field"


-----------------------------124853047628807
Content-Disposition: form-data; name="name"

test
-----------------------------124853047628807
Content-Disposition: form-data; name="surname"

test
-----------------------------124853047628807
Content-Disposition: form-data; name="age"

test
-----------------------------124853047628807--

In order to send this data, what I did is create a MultipartEntityBuilder like below:

    StringBody name = new StringBody("test", ContentType.MULTIPART_FORM_DATA);
    StringBody surname = new StringBody("test", ContentType.MULTIPART_FORM_DATA);
    StringBody age = new StringBody("test", ContentType.MULTIPART_FORM_DATA);
    StringBody field = new StringBody("", ContentType.MULTIPART_FORM_DATA);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.addPart("name", name);
    builder.addPart("surname", surname);
    builder.addPart("age", age);
    builder.addPart("field",field);


    return builder;

On top of that the headers that I'm sending are as follow:

 post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0");
        post.addHeader("Accept", "text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8");

I tried to set the multiform header but it doesn't work

post.addHeader("Content-type", "multipart/form-data");

Any advice on what I might be missing? THank you

cpu2007
  • 905
  • 4
  • 11
  • 24

3 Answers3

0

I know i had problems using my code that i have written in order to post some text and also some binary files and ended up writing it for myself the whole application/multipartform .

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundry);
        conn.setRequestProperty("Authorization", "----------------------------");



        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"user_id\""+lineend+lineend);
        out.writeBytes("1"+lineend);

        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"preview_id\""+lineend+lineend);
        out.writeBytes("1"+lineend);

        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"categories_id\""+lineend+lineend);
        out.writeBytes("2"+lineend);

        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"title\""+lineend+lineend);
        out.writeBytes("Mama"+lineend);

        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"tags\""+lineend+lineend);
        out.writeBytes("mama"+lineend);


        out.flush();


        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"video\"; filename=\""+file.getName()+"\""+lineend);
        out.writeBytes(lineend);
        Log.d("UPLOAD", "Titlul video-ului ="+file.getName());



        //decoding of bytes from video
        FileInputStream file_stream = new FileInputStream(file);
        bytesAvailable =file_stream.available();
        bufferSize = Math.min(bytesAvailable,maxBufferSize);
        buffer = new byte[bufferSize];
        Log.d("UPLOAD", "Bytes Read Video =" +bytesRead);

        bytesRead = file_stream.read(buffer);
        //writting to outputstream
        while (bytesRead >0){
            out.write(buffer, 0, bytesRead);
            bytesRead=file_stream.read(buffer);

        }
        Log.d("UPLOAD", "Done Loading first buffer");

        file_stream.close();

        out.writeBytes(twoHiphens+boundry+lineend);
        out.writeBytes("Content-Disposition: form-data; name=\"thumb\"; filename=\""+image.getName()+"\""+lineend);
        out.writeBytes(lineend);
        Log.d("UPLOAD", "Titlul preview-ului ="+image.getName());

        //decodint image bytes
        FileInputStream image_stream = new FileInputStream(image);
        int bytesRead2;
        int bytesAvailable2, bufferSize2 ;
        bytesAvailable2 = image_stream.available();
        bufferSize2 = Math.min(bytesAvailable2, maxBufferSize);
        byte []buffer2 = new byte[bufferSize2];


        //writing to outputstream
        bytesRead2 = image_stream.read(buffer2);
        while(bytesRead2>0){
            out.write(buffer2, 0, bytesRead2); //                   bytesAvailable2 = image_stream.available();
            bytesRead2 = image_stream.read(buffer2);
        }
        image_stream.close();

        Log.d("UPLOAD", "Done loading the second buffer");

        out.writeBytes(twoHiphens+boundry+twoHiphens+lineend);
        out.writeBytes(lineend);

        out.flush();
        out.close();



        Log.d("UPLOAD","Response Code = "+conn.getResponseCode());
        String responseMessage = conn.getResponseMessage();
        Log.d("UPLOAD", "Response Message  = "+responseMessage);


        InputStream  in;
        if(conn.getResponseCode() >= 400){
            in = conn.getErrorStream();
            }else{
                in = conn.getInputStream();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            StringBuilder response = new StringBuilder();
            char []bytes = new char[512];
            int read ;
            while((read = reader.read(bytes))!=-1){
                response.append(bytes, 0, read);
            }

            Log.d("UPLOAD", "Response " +response);


            conn.disconnect();

You can omit the code that referrers to binary data . Hope it would give you some help

Cristof
  • 33
  • 1
  • 5
  • tried your code Cristof but it doesn't work, it throws an error saying Request RejectedThe requested URL was rejected. Please consult with your administrator... – cpu2007 Jul 29 '14 at 15:01
  • I used it using REST API and for that i think you need to have and "Authorization" Header . I replaced my authorization code with "-----------------" – Cristof Jul 30 '14 at 17:52
0

Maybe the Content Type of each StringBody mustn't be ContentType.MULTIPART_FORM_DATA . Maybe it should be "text/plain "

Cristof
  • 33
  • 1
  • 5
0

Try this !

        File file = new File(path);


        File image = new File("/storage/emulated/0/DCIM/100MEDIA/a_thumbnail.jpg");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(urls[0]);
        HttpResponse response = null ;
        post.setHeader("Authorization","----------------------------");





        MultipartEntity ent = new MultipartEntity();
        try {

            ent.addPart("user_id",new StringBody("1"));
            ent.addPart("categories_id",new StringBody("3"));
            ent.addPart("tags",new StringBody("mama"));
            ent.addPart("title",new StringBody("mama"));
            ent.addPart("preview_id",new StringBody("2"));
            ent.addPart("thumb", new FileBody(image));
            ent.addPart("video", new FileBody(file));


            post.setEntity(ent);

            response = client.execute(post);

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            answer= answer+bufferedReader.readLine();

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return answer;
    }
Cristof
  • 33
  • 1
  • 5