What's wrong with this multipart form? Android-Java

Go To StackoverFlow.com

1

I always get back Content Length Required 411, what am I doing wrong here? am I not sending it right? Im sending to a RoR server. It basically will switch between statements depending on weather there is an image present or not. Thanks guys.

    public static void executeMultipartPost(Bitmap image, String topicid, String topost, Context c) throws Exception {
            String boundary = "------WebKitFormBoundary4QuqLuM1cE5lMwCy";

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            image.compress(CompressFormat.JPEG, 45, bos);
            byte[] data = bos.toByteArray();
            try
            {

                URL post_url = new URL("https://myapi");
                HttpURLConnection feed_connection = (HttpURLConnection) post_url.openConnection();
                feed_connection.setChunkedStreamingMode(0);
                feed_connection.setDoOutput(true);//make a POST Method as defualt is GET
                feed_connection.setRequestMethod("POST");
                feed_connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);


                //Now the actual composing of the multi part form
                String session_key = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"session_key\"\r\n\r\n" + key + "\r\n");
                String feed = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"feed\"\r\n\r\n" + topost + "\r\n");
                String sh_career = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"share_to_career_team\"\r\n\r\n" + 0 + "\r\n");
                String sh_friend = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"share_to_friend\"\r\n\r\n" + 0 + "\r\n");
                String url_t = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_title\"\r\n\r\n"+"\r\n");
                String url_d = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_description\"\r\n\r\n"+"\r\n");
                String url_a = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"url_address\"\r\n\r\n"+"\r\n");
                String topic_id = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"post_to\"\r\n\r\n" + topicid + "\r\n");


                if (data != null) {
                    String file_param_constant = "image";
                    Log.d("TAG","Image is here");
                    String image_part_1 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"richmedia_type\"\r\n\r\n" + "1\r\n");
                    String image_part_2 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + file_param_constant + "\"; filename=\"image.jpg\"\r\n" + "Content-Type: image/jpeg\r\n\r\n");
                    String missing_link = new String ("\r\n");
                    String image_part_3 = new String("--" + boundary + "--\r\n");
                    feed_connection.setRequestProperty("Content-Length", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length())));
                    Log.d("TAG", "CONTENT LEGNTH WITH IMG:"+String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length())));
                    ByteArrayOutputStream form_output   = new ByteArrayOutputStream();
                    OutputStream form_stream            = new BufferedOutputStream(feed_connection.getOutputStream());
                    form_output.write(session_key.getBytes());
                    form_output.write(feed.getBytes());
                    form_output.write(sh_career.getBytes());
                    form_output.write(sh_friend.getBytes());
                    form_output.write(url_t.getBytes());
                    form_output.write(url_d.getBytes());
                    form_output.write(url_a.getBytes());
                    form_output.write(topic_id.getBytes());
                    form_output.write(image_part_1.getBytes());
                    form_output.write(image_part_2.getBytes());
                    form_output.write(data);
                    form_output.write(missing_link.getBytes());

                    form_output.write(image_part_3.getBytes());

                    form_stream.write(form_output.toByteArray());
                    Log.d("TAG","Feed-Connection content legnth :"+Integer.toString(feed_connection.getContentLength()));

                }else {
                    String image_part_1 = new String("--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"richmedia_type\"\r\n\r\n" + "0\r\n");
                    Log.d("TAG", "CONTENT LEGNTH:"+String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length())));
                    feed_connection.setRequestProperty("Content-Length NO IMG", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + image.getByteCount() + topic_id.length())));
                    ByteArrayOutputStream form_output   = new ByteArrayOutputStream();
                    OutputStream form_stream            = new BufferedOutputStream(feed_connection.getOutputStream());
                    form_output.write(session_key.getBytes());
                    form_output.write(feed.getBytes());
                    form_output.write(sh_career.getBytes());
                    form_output.write(sh_friend.getBytes());
                    form_output.write(url_t.getBytes());
                    form_output.write(url_d.getBytes());
                    form_output.write(url_a.getBytes());
                    form_output.write(topic_id.getBytes());
                    form_output.write(image_part_1.getBytes());
                    form_stream.write(form_output.toByteArray());
                    Log.d("TAG","Feed-Connection content legnth :"+ Integer.toString(feed_connection.getContentLength()));
                }
                //write form to connection

                if (feed_connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                {
                    Log.d("TAG","CRAP BROKE MAN");
                    throw new Exception("Carp hit the fan an connection didnt get a OKAY Error Code: " + feed_connection.getResponseMessage() +  feed_connection.getResponseCode());
                }

                InputStream recieved_information = feed_connection.getInputStream();


            }
            catch (Exception the_e)
            {
                System.out.println("Not even" + the_e.getMessage());
                Log.d("TAG", "NOT EVEN" + the_e.getMessage());
            }
        }
2012-04-04 22:47
by user824015


2

Content Length is a Http Request Header Property, not a http Request Property.

This does not set the Content Length in Http Request Header:

feed_connection.setRequestProperty("Content-Length", String.valueOf((session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length())));

Try using HttpURLConnection.setFixedLengthStreamingMode() set the Content Length, for example:

feed_connection.setFixedLengthStreamingMode(session_key.length() + feed.length() + sh_career.length() + sh_friend.length() + url_t.length() + url_d.length() + url_a.length() + topic_id.length() + image_part_1.length() + image_part_2.length() + missing_link.length() + image_part_3.length());

Hope this helps.

2012-04-04 23:42
by yorkw
trying now thanks - user824015 2012-04-05 00:16
It seem's to have done something but now I get the error

"Already in chunked mode - user824015 2012-04-05 00:21

so I commented out feed_connection.setChunkedStreamingMode(0); and now I'm getting expected 1139 bytes but received 9714, so close - user824015 2012-04-05 00:26
I GOT IT!!! I needed to add data.length() in that string for setFixedLengthStreamingMode! thank you I love you all - user824015 2012-04-05 00:36
Buuut. Would you guys happen to know why sending a bitmap (taken from camera) would give me

D/TAG     ( 6780): CONTENT LEGNTH WITH IMG:3196

D/TAG ( 6780): Feed-Connection content legnth :-1 D/TAG ( 6780): Exception:unexpected end of stream - user824015 2012-04-05 00:53

Ads