Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 87
Registered: ‎07-07-2010
My Device: 8520
My Carrier: Student
Accepted Solution

Http post: post body contains less bytes than specified by content-length

Hi,

in my app I make an http post by using this code (fieldsToSend and referentiToSend are two vector initialized before)

 

                   HttpConnection conn = null;
        InputStream is = null;
        OutputStream os = null;


            conn = (HttpConnection) Connector.open(Constants.Synch.URL_NOTIFY);
            
            conn.setRequestMethod(HttpConnection.POST);
            
            URLEncodedPostData post = new URLEncodedPostData("UTF-8", false);
            

 

            for (int i = 0; i < fieldsToSend.size(); ++i) {
                String name = "visita" + (i + 1);
                String value = (String) fieldsToSend.elementAt(i);
                post.append(name, value);
            }
            
            for (int i = 0; i < referentiToSend.size(); ++i) {
                String name = "referente" + (i + 1);
                String value = (String) referentiToSend.elementAt(i);
                post.append(name, value);
            }

            conn.setRequestProperty("Content-Type", post.getContentType());
            conn.setRequestProperty("Content-Length", (new Integer(post.getBytes().length)).toString());
            
            os = conn.openOutputStream();
            os.write(post.getBytes());
            os.flush();
            
            int status = conn.getResponseCode();

           
            if (status == HttpConnection.HTTP_OK) {

                       // do something

            }

 

If I read server log I see this error

 

SRTServletReq E   SRVE0133E: An error occurred while parsing parameters. java.io.IOException: SRVE0216E: post body contains less bytes than specified by content-length

 

Can you tell me where is the problem?

 

Thanks in advance.

 

Regards,

Gianni.

 

Regular Contributor
Posts: 87
Registered: ‎07-07-2010
My Device: 8520
My Carrier: Student

Re: Http post: post body contains less bytes than specified by content-length

If I try with MultipartPostData instead of URLEndocedPostData I haven't error, but I'd like to use URLEndodedPostData beacause it happens that I work with special characters... Does anyone have a suggestion?

 

Regular Contributor
Posts: 87
Registered: ‎07-07-2010
My Device: 8520
My Carrier: Student

Re: Http post: post body contains less bytes than specified by content-length

[ Edited ]

Sorry... If I use MultipartPostData I don't get the exception but in log I see this:

 

InputStreamData: *ERR(java.io.IOException: SRVE0216E: post body contains less bytes than specified by content-length)*

Regular Contributor
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700
My Carrier: Airtel

Re: Http post: post body contains less bytes than specified by content-length

You can use WireShark (Network monitor tool) on your machine and see what exactly the content lenght  and data your application is sending.

 

Thanks

 

Regards

-Abhijit

Contributor
Posts: 14
Registered: ‎08-17-2010
My Device: Not Specified

Re: Http post: post body contains less bytes than specified by content-length

[ Edited ]
Why you are doing this? conn.setRequestProperty("Content-Length", (new Integer(post.getBytes().length)).toString());

Remove "new". It should be:
 conn.setRequestProperty("Content-Length", Integer
                    .toString(postBody.getBytes().length));
Regular Contributor
Posts: 87
Registered: ‎07-07-2010
My Device: 8520
My Carrier: Student

Re: Http post: post body contains less bytes than specified by content-length

Apologies... The problem was on the servlet, not on the BlackBerry. I'm too sorry...