奇葛格的BLOG

          紅塵最可笑,我自樂逍遙

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

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

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

          ? ? 當(dāng)我們以GET方式發(fā)送數(shù)據(jù)的時(shí)候,數(shù)據(jù)按照如下形式封裝成請(qǐng)求發(fā)送給服務(wù)器,我們可以看出數(shù)據(jù)都被包含在了URL中。
          ?GET /index.html?userid=joe&password=guessme HTTP/1.1
          ?Host: www.mysite.com
          ?User-Agent: Mozilla/4.0
          下面是我們?cè)贘2ME開發(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í)候,數(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ù)時(shí)候的代碼片斷
          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ù)值對(duì)。我們最好在聯(lián)網(wǎng)的時(shí)候設(shè)置這兩個(gè)Header,因?yàn)檫@樣服務(wù)器將很容易的知道將有什么類型的數(shù)據(jù),多少數(shù)據(jù)發(fā)送過來。

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

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

          評(píng)論

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 商城县| 新平| 建湖县| 六盘水市| 汾阳市| 桂平市| 饶河县| 富裕县| 雅江县| 通州区| 本溪市| 乐陵市| 林州市| 宜城市| 河南省| 都江堰市| 水城县| 自贡市| 汝阳县| 太湖县| 芮城县| 阿尔山市| 平陆县| 息烽县| 遂平县| 邳州市| 西宁市| 永州市| 北辰区| 金沙县| 收藏| 宾阳县| 乌鲁木齐县| 肇州县| 确山县| 九寨沟县| 玛沁县| 佳木斯市| 云南省| 弥渡县| 贡山|