Kxgx.com - 移动开发网

本站快讯:
搜索: 您的位置主页>参考源码>软件源码>>阅读源码:用户发送要求和接收服务器响应

用户发送要求和接收服务器响应

2006-11-20   来源:   作者:佚名   【 】 评论:0条

这是一个简单的联网信息服务代码,向服务器传送信息并获取服务器响应信息。其实这种代码也适用于聊天程序设计,向服务器发送信息,获取对方经服务器传来的信息。

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;

public class ViewFile extends MIDlet
{
  private String url = "http://www.corej2me.com/midpbook_v1e1/ch14/getHeaderInfo.txt";

  public void startApp()


  {
    try
    {
      processRequest();
    }
    catch (Exception e)
    {
      System.err.println("Msg: " + e.toString());
    }
  }    

  private void processRequest() throws IOException
  {
    HttpConnection http = null;
    InputStream iStrm = null;
    
    try
    {
      // Create the connection
      http = (HttpConnectionConnector.open(url);
      
      //----------------
      // Client Request
      //----------------
      // 1) Send request method


      http.setRequestMethod(HttpConnection.GET);
      
      // 2) Send header information (this header is optional)
      http.setRequestProperty("User-Agent""Profile/MIDP-1.0 Configuration/CLDC-1.0");
//      http.setRequestProperty("If-Modified-Since", "Mon, 16 Jul 2001 22:54:26 GMT");

      // If you experience IO problems, try 
      // removing the comment from the following line
      //http.setRequestProperty("Connection", "close");      
      
      // 3) Send body/data - No data for this request
      

      //----------------
      // Server Response


      //----------------
      System.out.println("url: " + url);
      System.out.println("-------------------------");      
      
      // 1) Get status Line
      System.out.println("Msg: " + http.getResponseMessage());                  

      System.out.println("Code: " + http.getResponseCode());                
      
      // 2) Get header information 
      if (http.getResponseCode() == HttpConnection.HTTP_OK)
      {
        System.out.println("field 0: " + http.getHeaderField(0));        
        System.out.println("field 1: " + http.getHeaderField(1));
        System.out.println("field 2: " + http.getHeaderField(2));        
        System.out.println("-------------------------");
                
        System.out.println("key 0: " + http.getHeaderFieldKey(0));        
        System.out.println("key 1 : " + http.getHeaderFieldKey(1));        
        System.out.println("key 2: " + http.getHeaderFieldKey(2));                
        System.out.println("-------------------------");
                                   
        System.out.println("content: " + http.getHeaderField("content-type"));
        System.out.println("date: " + http.getHeaderField("date"));


        System.out.println("last-modified: " + http.getHeaderField("last-modified"));                
        
        System.out.println("-------------------------");

        // 3) Get data (show the file contents)
        String str;
        iStrm = http.openInputStream();
        int length = (inthttp.getLength();
        if (length != -1)
        {
          // Read data in one chunk
          byte serverData[] new byte[length];
          iStrm.read(serverData);
          str = new String(serverData);

        }
        else  // Length not available...
        {
          ByteArrayOutputStream bStrm = new ByteArrayOutputStream();       
          
          // Read data one character at a time
          int ch;
          while ((ch = iStrm.read()) != -1)
            bStrm.write(ch);
  
          str = new String(bStrm.toByteArray());

          bStrm.close();                
        }
        
        System.out.println("File Contents: " + str);
        
        //-----------------------------
        // Show connection information


        //-----------------------------
        System.out.println("Host: " + http.getHost());
        System.out.println("Port: " + http.getPort());
        System.out.println("Type: " + http.getType());                
        
//        System.out.println("File: " + http.getFile());                        
//        System.out.println("Protocol: " + http.getProtocol());                        
//        System.out.println("URL: " + http.getURL());                                        


//        System.out.println("Query: " + http.getQuery());                                
      }
    }catch(Exception e){
       e.printStackTrace();
    
    }finally{

      // Clean up
      if (iStrm != null)
        iStrm.close();
      if (http != null)
        http.close();
    }


  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){ }
}

Tags:  
责任编辑:
  • 请文明参与讨论,禁止漫骂攻击。 用户名:新注册)密码:匿名:
    评论总数:0 [ 查看全部 ] 网友评论
    关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 帮助