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
Developer
Posts: 46
Registered: ‎12-01-2009
My Device: Not Specified
Accepted Solution

HTTP POST not working as expected

[ Edited ]

I can't figure out why the POST with data works when I use Firefox, but not when sent from the BlackBerry. The only difference appears to be that the BlackBerry sends less headers, but this is all legal HTTP.

 

This is the TCP data being sent to the server (from packet logger):

----------------------------------

POST /page HTTP/1.1
Host: localhost
Connection: close
Content-Length: 8


test=abc

----------------------------------

 

This appears to be correct. However, the webserver's $_POST (PHP) array is empty.

 

Code follows:

 

		HttpConnection c = (HttpConnection)Connector.open(connectionString);
		c.setRequestMethod(HttpConnection.POST);
		
		DataOutputStream out = c.openDataOutputStream();
		URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true);
		postData.append("test", "abc");
		
		System.out.println("requestData: " + new String(postData.getBytes()));
		out.write(postData.getBytes());
		//out.flush();
		
		c.getResponseCode();
		
		InputStream is = c.openDataInputStream();
		LineReader lr = new LineReader(is);
		
		while(true)
		{
			try
			{
				byte[] arr = lr.readLine();
				System.out.println("Line: " + new String(arr));
			}
			catch(EOFException e) { break; }
		}

 

Has anyone experienced similar problems using POST?

 

 

 

Cheers,

Pav

 

Update:

- Tried this on 2 unrelated webservers (Apache + PHP), both have the same problem.

 

 

Developer
Posts: 46
Registered: ‎12-01-2009
My Device: Not Specified

Re: HTTP POST not working as expected

SOLVED!

 

I've tried putting various headers that Firefox sends inside the BB request and this one made it work:

 

c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

 

Hope it saves someone out there a lot of frustration :-)

 

New Developer
Posts: 13
Registered: ‎02-07-2010
My Device: 8300 Simulator

Re: HTTP POST not working as expected

Hi, i am facing the same problem. Also tried your solution but no luck. Did you change anything else? or any suggestion? Thanks.

New Developer
Posts: 13
Registered: ‎02-07-2010
My Device: 8300 Simulator

Re: HTTP POST not working as expected

Ok never mind, i solved it. The problem was, i was using 'localhost' in my url, which should be the actual IP.