[?http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=101 ]
? ? 在MIDP2.0中提供了對TCP/IP層進行聯網開發的支持,但是這仍然需要設備廠商和運營商的支持,而HTTP連接是MIDP規范中規定必須支持的連接方式,因此在選擇開發聯網應用程序的時候,HTTP連接仍然是很有競爭力的方式。當然如果你選擇的目標設備支持Socket的話可以選擇Socket連接方式,本文主要介紹HTTP的兩種連接方式POST和GET的異同。
? ? HTTP協議是一種面向連接且無狀態的聯網方式,客戶端向服務器發送請求,服務器處理后把響應傳回客戶端就斷開連接。在我們選擇連接方式的時候主要有兩種可以選擇POST和GET。
? ? 當我們以GET方式發送數據的時候,數據按照如下形式封裝成請求發送給服務器,我們可以看出數據都被包含在了URL中。
?GET /index.html?userid=joe&password=guessme HTTP/1.1
?Host: www.mysite.com
?User-Agent: Mozilla/4.0
下面是我們在J2ME開發中通過GET方式發送數據的代碼片斷
?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
?}
? ? ?當我們使用POST方式發送數據的時候,數據被封裝在URL和Header后面,中間以空行來分隔。例如
?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
下面是我們按照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-supplied
?try {
? conn = (HttpConnection) Connector.open( url );
? conn.setRequestMethod( HttpConnection.POST );
? conn.setRequestProperty( "User-Agent", agent );
? conn.setRequestProperty( "Content-Type", 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
?}
?
? ? 從上面的代碼我們可以看出,如果使用POST方法,通常我們應該設置一些Headers,可以通過setRequestProperty()方法完成,其中 Content-Type和Content-Length是非常重要的,在MIDP中經常使用的Content-Type是 application/octet-stream和application/x-www-form-urlencoded,前者用于發送二進制數據,后者可以用于發送屬性-數值對。我們最好在聯網的時候設置這兩個Header,因為這樣服務器將很容易的知道將有什么類型的數據,多少數據發送過來。
? ? 在使用POST方法發送數據的時候,通常要涉及到io的知識,我們需要打開流,發送數據,關閉流。例如
? ? void postViaHttpConnection(String url) throws IOException {
? ? ? ? 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 are stored until
? ? ? ? ? ? // requested.
? ? ? ? ? ? is = c.openInputStream();
? ? ? ? ? ? // Get the ContentType
? ? ? ? ? ? String type = c.getType();
? ? ? ? ? ? processType(type);
? ? ? ? ? ? // Get the length and process the data
? ? ? ? ? ? int 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();
? ? ? ? }
? ? }
? ? ?通過如上的比較,我們可以看出POST方法發送數據的時候將更加靈活,你可以發送二進制數據,甚至可以實現對象的序列化。而使用GET方式發送數據的時候我們只能把數據在URL中發送出去,如果參數過多則很不方便,還要受到URL長度的限制,因此在J2ME聯網中我們推薦HTTP協議的POST方式。