在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方式。
分享到:
相关推荐
NULL 博文链接:https://2528.iteye.com/blog/1066483
j2me使用udp的简单连接源码 server与Client的连接
J2me中对HTTP 1.2中定义的KEEPALIVE支持得并不是很好,并不能获得一个http的长连接。对于JSE中http connection的实现还没验证。 使用Socket的一个好处是可以避免为每个网络请求建立单独的Connection。并且因为...
J2me中文教程.pdf J2me中文教程.pdf
j2me 实现抛物线的两种方法 j2me 实现抛物线的两种方法 j2me 实现抛物线的两种方法 j2me 实现抛物线的两种方法 j2me 实现抛物线的两种方法
用J2ME与ASP建立数据库连接 J2ME是利用HttpConnection建立HTTP连接,然后获取数据,ASP也是利用HTTP协议,因而可以利用J2ME与ASP建立连接,从而访问数据库。
J2me RMS存储 网络连接 详解 PDF文档 自制目录
j2me 文档 开发 官方 中文 j2me 文档 开发 官方 中文 j2me 文档 开发 官方 中文
J2ME中文教程 不错的学习资料,刚开始学习的可以参考下!
J2ME开发 J2ME开发 J2ME开发 J2ME开发
j2me http demo,几个小程序案例
j2me中文教程
J2ME 中文api以及应用 J2ME 中文api以及应用.rar
J2ME中文教程 J2ME中文教程
j2me教程,比较基础,容易掌握,很容易上手,对初学者很有帮助。。
本教程首先介绍了 j2me 开发体系,然后深入各个MIDP2.0 API,最后是搭建平台的知识。 第一章 “J2ME 技术概述”让你在学习J2ME 以前知道什么是J2ME。本章介绍了J2ME 平 台的体系结构和MIDlet 生命周期的概念。为以后...
J2ME中文API J2ME中文API帮助文档
J2ME 中文教程
J2ME API J2ME API J2ME API J2ME API J2ME API
J2ME三种低级用户界面事件处理技术比较