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

Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900
Accepted Solution

Empty POST body using OutputStream.write



I'm having trouble with the OutputStream write method. Specifically, the POST body is empty even though I specify data in the write() method. An HTTP request is made, I see it in the server logs, and I get a return status of 200 OK, but the data is just never sent. Any ideas?


Here is the code in question: (I'm overrideing the inputFromUser variable for testing purposes)


public boolean fetch(String inputFromUser) throws IOException {
		System.out.println("Fetch called with input " + inputFromUser);
		boolean contentAvailable = false;
		HttpConnection httpConn = null;
		inputFromUser = "name=javabb";

		httpConn = (HttpConnection)Connector.open(TEST_URL+get_connection_directive(), Connector.READ_WRITE);
		httpConn.setRequestProperty("Accept", "*/*");
		httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate, compress");
		httpConn.setRequestProperty("Content-Length", Integer.toString(inputFromUser.length()));
		httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		OutputStream output = null;
		output = httpConn.openOutputStream();
		// Getting the response code will open the connection,
        // send the request, and read the HTTP response headers.
        // The headers are stored until requested.			
		int status = 0;
		status = httpConn.getResponseCode();			
		if (status == HttpConnection.HTTP_OK)
			InputStream input = null;
			input = httpConn.openInputStream();
			if(httpConn.getEncoding().toLowerCase().equals("gzip")) {
				input = new GZIPInputStream(input);
			// Check to see if there's content
			int contentLength = (int) httpConn.getLength();
			byte[] bytes = null;
			bytes = IOUtilities.streamToBytes(input);
			StringBuffer raw = new StringBuffer(new String(bytes, "UTF-8"));                        
			if ( contentLength > 0 )
				contentAvailable =  true;
				System.out.println("Response: " + raw.toString());
			else {
				System.out.println("Content-length < 1");
			System.out.println("HTTP Status of: " + status);
			contentAvailable = false;

		return contentAvailable;


Posts: 17,019
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Empty POST body using OutputStream.write

get the length from the bytes. i would also omit the accept headers.
if you have control over the server check its logs, otherwise i would suggest that you test with your own server (like mini http server) to see what happens.
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Empty POST body using OutputStream.write

You don't close the output stream.  I suspect that might have something to do with the problem. 


Also there a couple of changes I would suggest that may be contributing to your problem.


1) You say:

httpConn.setRequestProperty("Content-Length", Integer.toString(inputFromUser.length()));


now inputFromUser is a String.  Its length is not necessairly the same as the number of bytes you will get when you convert it to bytes.  So you should really convert it to bytes and then find the length.


2) This code:


will convert the String using the BlackBerry default ISO8859-1 (might have remembered that incorrectly).  This will convert most Latin type characters, but does not work for special characters and other languages.  You need to choose a code that matches what the Server expects, which is typically UTF-8.


With these two changes, your code would look something like:


byte [] postBytes = null;

try {

postBytes = inputFromUser.getBytes("UTF-8");

} catch (Exception e) {

// can't happen with UTF-8


httpConn.setRequestProperty("Content-Length", Integer.toString(postBytes.length));


and then later



Let us know if these three changes (close, UTF-8 and setting the length correctly) resolve your problem. 

Posts: 40
Registered: ‎06-04-2014
My Device: Bold 9900

Re: Empty POST body using OutputStream.write

Thank you. The issue was with me sending the String length and not the length of the byte representation of the string.