Chinaunix首页 | 论坛 | 博客
  • 博客访问: 459987
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: Java

2010-06-08 17:40:46

在MIDP2.0中提供了对TCP/IP层进行联网开发的支持,但是这仍然需要设备厂商和运营商的支持,而HTTP连接是MIDP规范中规定必须支持的连接方式,因此在选择开发联网应用程序的时候,HTTP连接仍然是很有竞争力的方式。当然如果你选择的目标设备支持Socket的话可以选择Socket连接方式,本文主要介绍HTTP的两种连接方式POST和GET的异同。     HTTP协议是一种面向连接且无状态的联网方式,客户端向服务器发送请求,服务器处理后把响应传回客户端就断开连接。在我们选择连接方式的时候主要有两种可以选择POSTGET。      

当我们以GET方式发送数据的时候,数据按照如下形式封装成请求发送给服务器,我们可以看出数据都被包含在了URL中。 GET /index.html?userid=joe&password=guessme HTTP/1.1 Host: User-Agent: Mozilla/4.0下面是我们在J2ME开发中通过GET方式发送数据的代码片断: HttpConnection conn = null; String url = "" +              "/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: 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 = ""; 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 gamesn".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方式。

阅读(598) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~