奇葛格的BLOG

          紅塵最可笑,我自樂逍遙

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

          [?http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=101 ]
          ? ? 在MIDP2.0中提供了對TCP/IP層進(jìn)行聯(lián)網(wǎng)開發(fā)的支持,但是這仍然需要設(shè)備廠商和運營商的支持,而HTTP連接是MIDP規(guī)范中規(guī)定必須支持的連接方式,因此在選擇開發(fā)聯(lián)網(wǎng)應(yīng)用程序的時候,HTTP連接仍然是很有競爭力的方式。當(dāng)然如果你選擇的目標(biāo)設(shè)備支持Socket的話可以選擇Socket連接方式,本文主要介紹HTTP的兩種連接方式POST和GET的異同。

          ? ? HTTP協(xié)議是一種面向連接且無狀態(tài)的聯(lián)網(wǎng)方式,客戶端向服務(wù)器發(fā)送請求,服務(wù)器處理后把響應(yīng)傳回客戶端就斷開連接。在我們選擇連接方式的時候主要有兩種可以選擇POST和GET。

          ? ? 當(dāng)我們以GET方式發(fā)送數(shù)據(jù)的時候,數(shù)據(jù)按照如下形式封裝成請求發(fā)送給服務(wù)器,我們可以看出數(shù)據(jù)都被包含在了URL中。
          ?GET /index.html?userid=joe&password=guessme HTTP/1.1
          ?Host: www.mysite.com
          ?User-Agent: Mozilla/4.0
          下面是我們在J2ME開發(fā)中通過GET方式發(fā)送數(shù)據(jù)的代碼片斷
          ?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
          ?}

          ? ? ?當(dāng)我們使用POST方式發(fā)送數(shù)據(jù)的時候,數(shù)據(jù)被封裝在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方式發(fā)送數(shù)據(jù)時候的代碼片斷
          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方法,通常我們應(yīng)該設(shè)置一些Headers,可以通過setRequestProperty()方法完成,其中 Content-Type和Content-Length是非常重要的,在MIDP中經(jīng)常使用的Content-Type是 application/octet-stream和application/x-www-form-urlencoded,前者用于發(fā)送二進(jìn)制數(shù)據(jù),后者可以用于發(fā)送屬性-數(shù)值對。我們最好在聯(lián)網(wǎng)的時候設(shè)置這兩個Header,因為這樣服務(wù)器將很容易的知道將有什么類型的數(shù)據(jù),多少數(shù)據(jù)發(fā)送過來。

          ? ? 在使用POST方法發(fā)送數(shù)據(jù)的時候,通常要涉及到io的知識,我們需要打開流,發(fā)送數(shù)據(jù),關(guān)閉流。例如
          ? ? 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方法發(fā)送數(shù)據(jù)的時候?qū)⒏屿`活,你可以發(fā)送二進(jìn)制數(shù)據(jù),甚至可以實現(xiàn)對象的序列化。而使用GET方式發(fā)送數(shù)據(jù)的時候我們只能把數(shù)據(jù)在URL中發(fā)送出去,如果參數(shù)過多則很不方便,還要受到URL長度的限制,因此在J2ME聯(lián)網(wǎng)中我們推薦HTTP協(xié)議的POST方式。

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

          評論

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 上饶市| 察雅县| 冕宁县| 买车| 诸城市| 罗城| 庄浪县| 土默特左旗| 岫岩| 即墨市| 大余县| 宣恩县| 普格县| 社旗县| 江永县| 来安县| 大兴区| 广河县| 徐闻县| 正蓝旗| 崇信县| 威远县| 卓资县| 陕西省| 安庆市| 昔阳县| 嘉禾县| 怀来县| 隆昌县| 芮城县| 万载县| 甘德县| 沈阳市| 望江县| 灵山县| 上思县| 三台县| 岳池县| 萨嘎县| 于田县| 道真|