Passing Parameter using HTTP Post on Blackberry
Well, passing parameter using HTTP Get method is very simple. You manually put your parameter on your url like: http://www.google.co.id/search?ie=UTF-8&q=remoQte. On that url, there are 2 parameters, ie and q where ie’s value is UTF-8 and q’s value is remoQte. But sometimes, you don’t want to explicitly put your parameter on the url (maybe the parameter value is some sensitive data, like username, password, credit-card, etc). So, there is another approach and this approach use POST method as passing parameter. Instead of passing parameter via url, POST method passing parameter via Body message on HTTP (or HTTPS, depends on the protocol used by the server) request.
I will explain how to pass the parameter using POST method on Blackberry.
This is how it looks like (my explanation is inline with the source code below):
URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
//passing q’s value and ie’s value
postData.append("q", "remoQte");
postData.append("ie", "UTF-8");
ConnectionFactory conFactory = new ConnectionFactory();
ConnectionDescriptor conDesc = null;
try{
conDesc = conFactory.getConnection("http://www.google.co.id/search");
}catch(Exception e){
System.out.println(e.toString()+":"+e.getMessage());
}
String response = ""; // this variable used for the server response
// if we can get the connection descriptor from ConnectionFactory
if(null != conDesc){
try{
HttpConnection connection = (HttpConnection)conDesc.getConnection();
//set the header property
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Length", Integer.toString(postData.size())); //body content of post data
connection.setRequestProperty("Connection", "close"); // close the connection after success sending request and receiving response from the server
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // we set the content of this request as application/x-www-form-urlencoded, because the post data is encoded as form-urlencoded(if you print the post data string, it will be like this -> q=remoQte&ie=UTF-8).
//now it is time to write the post data into OutputStream
OutputStream out = connection.getOutputStream();
out.write(postData.getBytes("UTF-8"));
out.flush();
out.close();
int responseCode = connection.getResponseCode(); //when this code is called, the post data request will be send to server, and after that we can read the response from the server if the response code is 200 (HTTP OK).
if(responseCode == HttpConnection.HTTP_OK){
//read the response from the server, if the response is ascii character, you can use this following code, otherwise, you must use array of byte instead of String
InputStream in = connection.getInputStream();
StringBuffer buf = new StringBuffer();
int read = -1;
while((read = in.read())!= -1)
buf.append((char)read);
response = buf.toString();
}
//don’t forget to close the connection
connection.close();
}catch(Exception e){
System.out.println(e.toString()+":"+e.getMessage());
}
}

Recent Comments