buerkai 发表于 2013-1-26 13:32:59

j2me联网

HTTP协议是一种面向连接且无状态的联网方式,客户端向服务器发送请求,服务器处理后把响应传回客户端就断开连接。在我们选择连接方式的时候主要有两种可以选择POST和GET。

当我们以GET方式发送数据的时候,数据按照如下形式封装成请求发送给服务器,我们可以看出数据都被包含在了URL中。
GET: index.html?userid=joe&password=guessme HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
代码实现:
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
代码实现:
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 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;

            int actual = is.read(data);

            process(data);

      } else {

            int ch;

            while ((ch = is.read()) != -1) {

                process((byte)ch);

            }

      }

    }


    finally {

      f (is != null)

            is.close();

      if (os != null)

            os.close();

      if (c != null)

            c.close();

    }

}   

通过如上的比较,我们可以看出POST方法发送数据的时候将更加灵活,你可以发送二进制数据,甚至可以实现对象的序列化。而使用GET方式发送数据的时候我们只能把数据在URL中发送出去,如果参数过多则很不方便,还要受到URL长度的限制,因此在J2ME联网中我们推荐HTTP协议的POST方式。
页: [1]
查看完整版本: j2me联网