Http POST in BlackBerry

十年热恋 提交于 2019-11-27 02:11:36
 ...
httpConn = (HttpConnection)connDesc.getConnection();    
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("username",name);
httpConn.setRequestProperty("password",pass);
....

What type of a POST do you use? If you are just passing key-value pairs, then it should be a POST of a "application/x-www-form-urlencoded" content-type.

So, what lacks youe code is:

1). Set a proper content-type on your connection:

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

2). Prepare the content to be sent to the server via the POST:

URLEncodedPostData encPostData = new URLEncodedPostData("UTF-8", false);
encPostData.append("username", username);
encPostData.append("password", password);
encPostData.append("age", age);
byte[] postData = encPostData.toString().getBytes("UTF-8");

3). Set content-length for the connection (this step may be optional - try without this first, probably the BB OS is smart enough to set this automatically):

httpConn.setRequestProperty("Content-Length", String.valueOf(postData.length));

4). Open an OutputStream and write the content to it (the code is simplified):

OutputStream os = httpConn.openOutputStream();
os.write(postData);
os.flush();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!