Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 13:10, 5 June 2009 by tanjaluodes (Talk | contribs)

How to send POST data to a web server

From Wiki
Jump to: navigation, search

Using Java ME with MIDP 1.0 or MIDP 2.0 you can send and receive information from/to a webserver using HTTP protocol. If you want to send data using POST method, you should use this code:

First load the required libraries:


Then use this code in a function:

HttpConnection c = (HttpConnection)"");
byte[] data;
// data should be filled with binary data to send
c.setRequestProperty("Content-Length", Integer.toString(data.length));
OutputStream sending = c.openOutputStream();

If you want to send POST parameters to be read by PHP, ASP.NET or other server platform, you should make a string parameter like this

// This is a sample
String strData = "name=" + game.getName() + "&score=" + game.getScore();
byte[] data = strData.getBytes();

And also, you have to define an HTTP parameter like this:

c.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
133 page views in the last 30 days.