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
New Contributor
eloka
Posts: 9
Registered: ‎07-21-2011
My Device: developer
Accepted Solution

Post Request

hello guys, so far my experience build a BB app has been interesting now am stuck with a problem that should be very simple but i dont know what am doin wrong. am trying to send login data to the server using POST method. have seached online and tried every useful info i got but still the same problem. the connection and everything works greate but the data is not sent. when i check my apache error log i see that the post parameter is undefined. GET method works perfectly but not POST. could it be because am running it on a simulator or is it or does setting deviceside=true have anything to do with it. would really appreciate some help here. below is a portion of the code am using.

 

private void postURL() {
String postString = postDataField.getText();
URLEncodedPostData encodedData = new URLEncodedPostData(null, false);
encodedData.append("content", postString);
HttpRequestDispatcher dispatcher = new HttpRequestDispatcher(urlField
.getText(), "POST", this, encodedData.getBytes());
dispatcher.start();
}

 

public HttpRequestDispatcher(String url, String method, NetworkingMainScreen screen, byte[] postData){
this.url = url;
this.method = method;
this.screen = screen;
this.postData = postData;
}

public void run() {
try {
HttpConnection connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod(method);
if (method.equals("POST") && postData != null) {
connection.setRequestProperty("Content-type", "application/xwww-form-urlencoded");
OutputStream requestOutput = connection.openOutputStream();
requestOutput.write(postData);
requestOutput.close();
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
screen.requestFailed("Unexpected response code: " + responseCode);
connection.close();
return;
}

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Post Request

"could it be because am running it on a simulator or is it or does setting deviceside=true have anything to do with it"

 

No, POST only changes the format of the data sent, if a GET works, a POST should work. 

 

Does your BB processing actually correctly detect the post data?  In other words do you actually do through this:

connection.setRequestProperty("Content-type", "application/xwww-form-urlencoded");

...

You can debug or use logging output to check. 

 

Assuming it does, then my immediate suspicion is that there is some header missing that the Server is expecting to see on POST requests.  The most likely is the length of the data, so you could add the following line:

 

connection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postData.length));

 

Let us know if that helps.

Please use plain text.
New Contributor
eloka
Posts: 9
Registered: ‎07-21-2011
My Device: developer

Re: Post Request

thanks for your reply. it showed me something i wasnt seeing in my code before. i was missing a dash in  "application/xwww-form-urlencoded" so i changed it to  "application/x-www-form-urlencoded" and included the header as well and everything works great. thanks

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Post Request

Good spot.

 

I recommend that you use the supplied constants, look at HttpProtocolConstants class. 

Please use plain text.