In MIDP 2.0, it provides support for networking development of TCP / IP layers, but this still requires device manufacturers and operations.
Support for merchants, and HTTP connection is the way to support the MIDP specification, so in selecting the development networking application
When the order is, the HTTP connection is still very competitive. Of course, if you choose the target device to support socket
You can choose the Socket connection method. This article mainly introduces the two connection methods of HTTP POST and GET.
The HTTP protocol is a connection-free networking method, and the client sends a request to the server, and the server is processed.
Disconnect the response back to the client. There are two main types of POSTGETs that can be selected when we choose to connect.
When we send data in a GET, the data is packaged into a request to send to the server as follows.
It can be seen that the data is included in the URL.
Get /index.html?userid=joe&password=guessme http / 1.1
Host: www.mysite.com
User-agent: mozilla / 4.0
Here is the code snippet we send data in J2ME development:
HttpConnection conn = null;
String Url = "http://www.mysite.com"
"/index.html?userid=joe&password=guessme";
String Agent = "Mozilla / 4.0";
Try {
CONN = (httpConnection) Connector.open (URL);
Conn.setRequestProperty ("User-Agent", Agent;
Int rc = conn.getResponsecode ();
... // Process IT
}
Catch (IOException E) {
// Handle The Error Here
}
When we use the POST mode to send data, the data is packaged behind the URL and Header, in the middle.
Separate. E.g
Post / login.jsp http / 1.1
Host: www.mysite.com
User-agent: mozilla / 4.0
Content-Length: 27
Content-Type: Application / X-WWW-FORM-URLENCODED
Userid = Joe & Password = Guessme
Below is the code snippet we sending data according to the POST:
HttpConnection conn = null;
String Url = "http://www.mysite.com/login.jsp";
String Agent = "Mozilla / 4.0";
String Rawdata = "Userid = Joe & Password = Guessme";
String type = "Application / X-WWW-FORM-URLENCODED";
String encodeddata = encode (rawdata); // user-support
Try {
CONN = (httpConnection) Connector.open (URL);
Conn.setRequestMethod (httpconnection.post);
Conn.setRequestProperty ("User-Agent", Agent;
Conn.setRequestProperty ("Content-Type"; Conn.setrEquestProperty ("Content-Length",
Encodeddata.length ());
OutputStream Os = Conn.OpenOutputStream ();
Os.write (EncodedData.getbytes);
Int rc = conn.getResponsecode ();
... // Process IT
}
Catch (IOException E) {
// Handle The Error Here
}
From the above code we can see that if you use the Post method, we should usually set some headers, you can
Completed by the setRequestProperty () method, where content-type and content-length are very important
Content-Type, often used in MIDP is Application / OcTet-Stream and Application / X-WWW-
Form-urlencoded, for the former to send binary data, which can be used to send attributes-value pairs. We best
Set these two headers when the network is, because the server will easily know what type of data will be, how much data
Send it.
When using the POST method to send data, usually involve IO knowledge, we need to open flow, send materials,
Close the stream. E.g
Void PostViahttpConnection (String Url) throws oException {
HttpConnection C = NULL;
InputStream IS = NULL;
OutputStream OS = NULL;
Try {
C = (httpConnection) Connector.Open (URL);
// set the request method and Headers
C.SetRequestMethod (httpConnection.post);
C.SetRequestProperty ("if-modified-since",
"29 OCT 1999 19:43:31 GMT");
C.SetRequestProperty ("User-Agent",
"Profile / MIDP-1.0 Configuration / CLDC-1.0");
C.SetRequestProperty ("Content-Language", "EN-US");
// Getting the Output Stream May Flush The Headers
Os = c.OpenOutputStream ();
Os.write ("List games / n" .getbytes ());
Os.flush (); // Optional, OpenInputStream Will Flush
// Opening The InputStream Will Open THE Connection
// and read the http headers. They area stored unsil
// Requester.
IS = C.OpenInputStream ();
// Get the contenttype
String type = c.gettype ();
Processtype (TYPE);
// Get the length and process the dataint len = (int) c.getlength ();
IF (len> 0) {
Byte [] data = new byte [len];
Int actual = is.read (data);
Process (data);
} else {
int CH;
While ((ch = is.read ())! = -1) {
PROCESS (BYTE) CH);
}
}
} finally {
IF (is! = null)
Is.close ();
IF (OS! = NULL)
Os.Close ();
IF (c! = null)
C. close ();
}
}
With the above comparison, we can see that the post method will be more flexible when sending data, you can send binary
Data can even achieve sequentialization of objects. When using the GET method, we can only put data in the URL.
Send it out, if the parameters are too much, it is very inconvenient, but also subject to the length of the URL, so we push in J2ME networking
Recommend the POST mode of the HTTP protocol.