Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31586
  • 博文数量: 19
  • 博客积分: 410
  • 博客等级: 下士
  • 技术积分: 132
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-30 13:33
文章分类
文章存档

2012年(19)

我的朋友

分类: 嵌入式

2012-03-30 14:45:25

1.http协议
HTTP协议在发送时可以根据传输数据的方式一般分为get、post两种方法。Get方法和post方法的区别是传送数据时,数据所放置的位置不同。Get方法把参数放置在协议内部的请
求行中。而post方法将提交的参数放置在实体数据部分中。
HTTP协议要分为三个部分:
  a) 请求行/响应行:该部分只有一行,包含三个部分:请求方法 请求资源 使用的HTTP协议的版本。例如:get /index.html http/1.1, 其中get为请求方法,/index.html是请求的资源,http/1.1是指客户端支持的HTTP协议的版本。
  b) 头信息:头信息部分主要传输服务器或者客户端的一些设定信息,可以有任意多个,传输时使用“名称:值”的形式,如下:
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Accept", "application/octet-stream");
conn.setRequestProperty("Connection", "close");
 c) 实体数据


2.使用J2ME进行无线网络编程
定义在javax.microedition.io类中的抽象网络和文件输入输出框架称为通用连接框架(Generic Connection Framework,简称GCF)。GCF定义了一套有关抽象化的内容来描述不同的通信方法。最高级的抽象被称作连接(Connection),还声明了六个接口(四个是直接的,两个是间接的)。这七个接口就构成了J2ME的CLDC的一部分,CLDC是大多数的能使用Java的无线设备使用的配置。设计这个配置的目的就是为所有的CLDC设备(手提电话,双向传呼机,低档的PDA等等)提供公用的网络和文件输入输出能力。
由于移动终端自身的限制,所支持的网络协议非常有限,仅限于HTTP,Socket,UDP等几种协议。不同的厂家可能还支持其他网络协议,但是,MIDP 1.0规范规定,HTTP协议是必须实现的协议,而其他协议的实现都是可选的。因此,为了能在不同类型的手机上移植,所以在移动应用开发时应尽量采用HTTP作为网络连接的首选协议,这样还能重用服务器端的代码。
GCF的关键在于把所有的连接都按照url来处理,这样容易从Connector中得到不同类型的连接,如HttpConnection和SocketConnection.


3.J2ME使用http协议进行网络通信
3.1.发送一个HTTP GET请求
1)使用Connector类打开一个到服务器的连接
2)得到HttpConnection上的一个DataInputStream,用来读取服务器的响应数据。
3)将读取到的服务器的响应数据放入缓存
Java代码 复制代码 收藏代码
  1. public String sendHttpGet(String url) throws IOException {
  2. HttpConnection hcon = null;
  3. DataInputStream dis = null;
  4. String message = "";
  5. try {
  6. hcon = (HttpConnection) Connector.open(url);
  7. dis = new DataInputStream(hcon.openInputStream());
  8. int ch;
  9. message = dis.readUTF();
  10. } finally {
  11. if (hcon != null)
  12. hcon.close();
  13. if (dis != null)
  14. dis.close();
  15. }
  16. return message;
  17. }
public String sendHttpGet(String url) throws IOException { HttpConnection hcon = null; DataInputStream dis = null; String message = ""; try { hcon = (HttpConnection) Connector.open(url); dis = new DataInputStream(hcon.openInputStream()); int ch; message = dis.readUTF(); } finally { if (hcon != null) hcon.close(); if (dis != null) dis.close(); } return message; }
使用GET方式发送数据的时候数据只能通过URL传送到服务器,数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使
用“&”连接;如:,
使用GET方式发送数据,参数还要受到URL长度的限制,不能超过256字
Get是HttpConnection 的默认方式
使用GET方式发送数据,用户可以在浏览器上直接看到提交的数据,数据安全性不高

3.2.发送一个HTTP POST请求
处理过程和发送一个HTTP GET请求的处理非常类似,但稍稍复杂些。步骤如下:
1)设置HttpConnection的请求方式为
2)使用Connector类打开一个到服务器的连接,打开时将连接设置为Connector.READ_WRITE 方式,允许客户端可以通过连接在服务器上读和写
3)得到一个用于现有的HTTP连接的DataOutputStream对象。
4)向指定的DataOutputStream写数据
5)得到HttpConnection上的一个DataInputStream,用来读取服务器的响应数据。
3)将读取到的服务器的响应数据放入缓存
Java代码 复制代码 收藏代码
  1. public String sendHttpGet(String url,String requestBody) throws IOException {
  2. HttpConnection hcon = null;
  3. DataInputStream dism = null;
  4. OutputStream os = null;
  5. String message = "";
  6. try {
  7. // Connector.READ_WRITE参数允许客户端可以通过连接在服务器上读和写。
  8. hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
  9. // 设置HttpConnection对象使用的请求方法为POST
  10. hcon.setRequestMethod(HttpConnection.POST);
  11. // 得到一个用于现有的HTTP连接的OutputStream对象。
  12. os = hcon.openOutputStream();
  13. byte[] byteRequest = requestBody.getBytes();
  14. // 向指定的OutputStream写数据
  15. os.write(byteRequest);
  16. // 得到Http响应代码
  17. int rc = hcon.getResponseCode();
  18. // 正常响应
  19. if (rc == HttpConnection.HTTP_OK) {
  20. // 构建输入流
  21. dism = new DataInputStream(hcon.openInputStream());
  22. // 读取服务器返回的字节流
  23. String result = dism.readUTF();
  24. dism.close();
  25. // 判断
  26. if (result.equals("true")) {
  27. // 显示登录成功
  28. message = "success";
  29. } else if (result.equals("false")) {
  30. message = "fail";
  31. }
  32. }
  33. } finally {
  34. if (hcon != null)
  35. hcon.close();
  36. if (dism != null)
  37. dism.close();
  38. if (os != null)
  39. os.close();
  40. }
  41. return message.toString();
  42. }
public String sendHttpGet(String url,String requestBody) throws IOException { HttpConnection hcon = null; DataInputStream dism = null; OutputStream os = null; String message = ""; try { // Connector.READ_WRITE参数允许客户端可以通过连接在服务器上读和写。 hcon = (HttpConnection) Connector.open(url, Connector.READ_WRITE); // 设置HttpConnection对象使用的请求方法为POST hcon.setRequestMethod(HttpConnection.POST); // 得到一个用于现有的HTTP连接的OutputStream对象。 os = hcon.openOutputStream(); byte[] byteRequest = requestBody.getBytes(); // 向指定的OutputStream写数据 os.write(byteRequest); // 得到Http响应代码 int rc = hcon.getResponseCode(); // 正常响应 if (rc == HttpConnection.HTTP_OK) { // 构建输入流 dism = new DataInputStream(hcon.openInputStream()); // 读取服务器返回的字节流 String result = dism.readUTF(); dism.close(); // 判断 if (result.equals("true")) { // 显示登录成功 message = "success"; } else if (result.equals("false")) { message = "fail"; } } } finally { if (hcon != null) hcon.close(); if (dism != null) dism.close(); if (os != null) os.close(); } return message.toString(); }
本文欢迎转载,转载请注明出处和作者
阅读(709) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~