首页 | 手机 | 笔记本 | 硬件 | MP3 | 数码相机 | 品牌机 | 办公 | 网络 | 服务器 | 投影机 | 新闻 | 游戏 | 学院 | 下载 | 论坛 | 博客 | 商城 | 分站 | 北京 上海 广州 深圳
首页 >> 学院 >> 程序开发 >> 程序开发Java
在无线J2ME设备上实现超文本传输协议
2006-8-7 14:33:00 文/javajia 出处:javajia

  // 从服务器上取回响应
  int ch;
  while ( ( ch = dis.read() ) != -1 ) {
  responseMessage.append( (char) ch );
  }//结束while ( ( ch = dis.read() ) != -1 )
  }
  catch( Exception e )
  {
  e.printStackTrace();
  responseMessage.append( "ERROR" );
  } finally {
  try {
  if ( hcon != null ) hcon.close();
  if ( dis != null ) dis.close();
  } catch ( IOException ioe ) {
  ioe.printStackTrace();
  }//结束try/catch
  }//结束try/catch/finally
  return responseMessage.toString();
  }//结束sendHttpGet( String )

  private String sendHttpPost( String url )
  {
  HttpConnection hcon = null;
  DataInputStream dis = null;
  DataOutputStream dos = null;
  StringBuffer responseMessage = new StringBuffer();
  // 请求体
  String requeststring = "This is a POST.";

  try {
  // 使用读写权限的 HttpConnection
  hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );

  //设置请求方法为POST
  hcon.setRequestMethod( HttpConnection.POST );

  // 取得发送请求字符串的DataOutputStream
  dos = hcon.openDataOutputStream();
  byte[] request_body = requeststring.getBytes();

  // 发送请求字符串到服务器
  for( int i = 0; i < request_body.length; i++ ) {
  dos.writeByte( request_body[i] );
  }//结束 for( int i = 0; i < request_body.length; i++ )

  // 取得做为接收服务器响应的DataInputStream
  dis = new DataInputStream( hcon.openInputStream() );

  // 从服务器上取回响应
  int ch;
  while( ( ch = dis.read() ) != -1 ) {
  responseMessage.append( (char)ch );
  }//结束while( ( ch = dis.read() ) != -1 ) {
  }
  catch( Exception e )
  {
  e.printStackTrace();
  responseMessage.append( "ERROR" );
  }
  finally {
  // 释放输入输出流和HTTP连接
  try {
  if( hcon != null ) hcon.close();
  if( dis != null ) dis.close();
  if( dos != null ) dos.close();
  } catch ( IOException ioe ) {
  ioe.printStackTrace();
  }//结束try/catch
  }//结束try/catch/finally
  return responseMessage.toString();
  }//结束sendHttpPost( String )

  public void pauseApp() {
  }//结束pauseApp()

  public void destroyApp( boolean unconditional ) {
  myDisplay = null;
  requestScreen = null;
  requestField = null;
  resultScreen = null;
  resultField = null;
  }//结束 destroyApp( boolean )
  }//结束HttpMidlet


看本文的网友还看了:
热点关注:
·视频教程中心
·AutoCAD 2007
·Dreamweaver8
·Photoshop CS3
·PS图层全知道
·Flash Action Script
·数码照片处理大全
最新教程>>>
·Photoshop CS3视频教程:Vista壁纸自己做
·视频教程 Photoshop超超级去水印法
·Dreamweaver视频教程-制作网站三大原则
·AutoCAD 2007中文版标准视频教程—初识三维部
·AutoCAD 2007中文版标准视频教程—端盖(2)
·AutoCAD 2007中文版标准视频教程—建筑平面图
·GoLive视频教程—使用CSS编辑器之一
·GoLive视频教程—通过CSS+DIV进行布局之一
·学院频道热门文章
·Photoshop抠出复杂背景下的乱发
·利用Photoshop光照滤镜打造太阳初升
·eMule电骡长时间下载到底伤不伤硬盘
·打造最强的IE IE6“丰满”完全手册
·能否王者归来?RealPlayer11抢先体验
·“覆水可收” PC数据保镖V2.0体验报告
· OpenOffice VS MS Office大比拼
·一个网站全搞定 在网页上使用QQ和MSN
·学院专题
·一起分享2007年上半年精彩大片
·炎炎夏日 极尽视听播放器专题
·装饰设计视频教程:建筑渲染技巧
·我型我show 桌面大师群英会
·维护系统用它们!系统工具视频教程专集
·今日推荐
·看专题赢显卡 RV6X0全国市场直击报道
·百款笔记本直降千元 颐高暑促劲减风暴
·天兵神器 诺基亚智尊E90完全评测专题
·视觉的盛宴 影像器材与技术博览会直击
·我的坛子我做主 喷墨一体机版主挑战赛
2006 经营许可证编号: 粤B2-20050807