Revision as of 13:45, 25 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to send POST data to a web server

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet shows how you can send and receive information from/to a webserver using HTTP protocol's POST method using Java ME. It applies to Java ME with either MIDP 1.0 or MIDP 2.0.

Article Metadata
Created: firt (15 Apr 2007)
Last edited: hamishwillee (25 Jul 2013)

First load the required libraries:

import javax.microedition.io.*;
import java.io.*;

Then use this code in a function:

HttpConnection c = (HttpConnection) Connector.open("http://www.domain.com/url");
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");
This page was last modified on 25 July 2013, at 13:45.
255 page views in the last 30 days.