Motivation
For the
WorkingTime midlet I needed HTTP POST to get data from my mobile to the outer world. As the internet offers open formmail cgi scripts and services, I thought this would be easy to use…
Trap
All my emulators worked fine with code as below having the output stream flushed in the end. On my device and within the Sun J2ME Toolkit, the cgi call failed, because the server complained about content-length and chunked data… I searched a lot and finally got the info, that flush() within device implementations might cause unforseen data changing… So simply leave it away. It worked on my Nokia 6610 quite fine after that change, even 'larger' (600 Bytes) ones worked.
c = (HttpConnection) Connector.open(POST_URL);
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty(
"IF-Modified-Since",
"25 Nov 2001 15:17:19 GMT");
c.setRequestProperty(
"User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-CA");
c.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded");
os = c.openOutputStream();
os.write(postmsg);
// flush will cause message to be chunked on device! Webserver rejects!
//os.flush();
os.close();