×
Namespaces

Variants
Actions
(Difference between revisions)

How to send POST data to a web server

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
Line 1: Line 1:
[[Category:Java ME]][[Category:How To]][[Category:Networking on Java ME]][[Category:HTTP]][[Category:Code Snippet]]
+
[[Category:Networking on Java ME]][[Category:HTTP]][[Category:Code Snippet]][[Category:How To]]
 
{{Abstract|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]].}}
 
{{Abstract|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]].}}
  
Line 7: Line 7:
 
|devices= <!-- Devices tested against - e.g. ''devices=N95, N8'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=N95, N8'') -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords=HttpConnection
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->

Latest revision as of 13:45, 25 July 2013

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
Article
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");
c.setRequestMethod(HttpConnection.POST);
byte[] data;
// data should be filled with binary data to send
c.setRequestProperty("Content-Length", Integer.toString(data.length));
 
OutputStream sending = c.openOutputStream();
sending.write(data);
sending.close();

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.
208 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×