⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 j2me http连接测试程序。初学者非常有用
💻 JAVA
字号:

import java.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import java.util.Calendar;



import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;


public class test extends MIDlet{
 public String gLog="";
  public test() {
  }

  public void startApp() {
    Form a=new Form("test");
    Display.getDisplay(this).setCurrent(a);
    String s1= doPost("http://www.xm365.jp/wmctest.php", null);
    a.append("1:\n"+s1);
    a.append(gLog);
    String s2= doPost("http://nri.d2ml.com/wmctest.aspx", null);
    a.append("2:\n"+s2);
    a.append(gLog);
  }

  protected void destroyApp(boolean unconditional) {
  }

  public void pauseApp() {
  }

  public String doPost(String url, byte[] param) {
    HttpConnection connection = null;
    OutputStream os = null;
    InputStream in = null;
    String myResult = "", str, msg;
    boolean bError = false;
    byte[] rs = null;
    try {
      //愙懕丒彂偒崬傒
      connection = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
      connection.setRequestMethod(HttpConnection.POST);
      connection.setRequestProperty("Content-Type", "text/plain");
      os = connection.openOutputStream();
      os.close();
      gLog="";
      gLog = gLog + "#Content-Encoding=" + connection.getHeaderField("Content-Encoding");
      gLog = gLog + "#Content-Length=" +  connection.getHeaderField("Content-Length");
      gLog = gLog + "#Content-Type=" + connection.getHeaderField("Content-Type");
      gLog = gLog + "#XMID=" + connection.getHeaderField("XMID");

      int aCode = connection.getResponseCode();
      if (aCode == connection.HTTP_OK) {
        int nLen=Integer.parseInt(connection.getHeaderField("Content-Length"));
        //撉崬
        in = connection.openInputStream();
        if (nLen>0){
          rs = new byte[nLen];
          int w_char, count = 0;
          while ( (w_char = in.read()) != -1) {
            rs[count] = (byte) (w_char & 0x00FF);
            count++;
          }
        }else{
        }
      }
      else {
        bError = true;
        myResult = "ResponseCode Error:"+aCode;
      }
    }
    catch (Exception e) {
      myResult = "Error:"+e.toString();
      bError = true;
    }
    finally {
        try {
          if (os != null) {
            os.close();
            os = null;
          }
          if (in != null) {
            in.close();
            in = null;
          }
          if (connection != null) {
            connection.close();
            connection = null;
          }
        }
        catch (Exception ex) {
        }
    }
    if(bError){
      return myResult;
    }else{
      if (rs == null)
        return "";
      else{
        try{
          return new String(rs, "utf-8");
          //return new String(rs);
        }catch(Exception ex){return "utf8 error";}
      }
    }
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -