奇葛格的BLOG

          紅塵最可笑,我自樂逍遙

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            59 隨筆 :: 23 文章 :: 11 評論 :: 0 Trackbacks

          [?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方式。

          posted on 2006-08-23 15:38 奇葛格 閱讀(3172) 評論(1)  編輯  收藏 所屬分類: 網絡|設備,協議

          評論

          # re: 兩種HTTP連接方式POST&GET的比較[轉] 2016-01-09 19:48 22
          44  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 綦江县| 珠海市| 呈贡县| 三明市| 杭州市| 衡东县| 达州市| 威宁| 偏关县| 吉首市| 谷城县| 都昌县| 通许县| 苍南县| 湖州市| 广宗县| 岳普湖县| 宁海县| 敖汉旗| 澄江县| 佛山市| 高邮市| 宕昌县| 南召县| 太仓市| 临高县| 新乡市| 循化| 永福县| 江源县| 古蔺县| 黄大仙区| 汕尾市| 大足县| 鲁甸县| 黔西县| 桐梓县| 信宜市| 南康市| 永春县| 南京市|