奇葛格的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 奇葛格 阅读(3131) 评论(1)  编辑  收藏 所属分类: 网络|设备,协议

评论

# re: 两种HTTP连接方式POST&GET的比较[转] 2016-01-09 19:48 22
44  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: