`
mmdev
  • 浏览: 12915755 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

J2ME中两种Http连接方式Post&Get的比较

 
阅读更多

在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: 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方式。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics