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

📄 httpcookiemidlet.java

📁 透过Java编程设计的网站。包含很多功能点
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class HttpCookieMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  private Command sendCommand;
  private Command backCommand;
    //The display for this MIDlet
  private Display display;

  private String URL = "http://localhost/JIDCA/all_http.asp";
  private String sessionID;

  private Form mainForm,resultForm;
  private Item mItem;//联机成功时作为ImageItem,失败时作为StringItem
  private TextField URLField;

  public HttpCookieMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("离开",Command.EXIT,1);
    sendCommand = new Command("送出",Command.OK, 1);
    backCommand = new Command("上页",Command.OK,1);
   }

  // Start the MIDlet by creating the TextBox and
  // associating the exit command and listener.
  public void startApp() {
	URLField = new TextField(null,URL,250,TextField.URL);
    mainForm = new Form("请输入网址");
    mainForm.append(URLField);
    mainForm.addCommand(sendCommand);
    mainForm.addCommand(exitCommand);
    mainForm.setCommandListener(this);
    display.setCurrent(mainForm);
  }

  // Pause is a no-op because there are no background
  // activities or record stores to be closed.
  public void pauseApp() { }

  // Destroy must cleanup everything not handled
  // by the garbage collector.
  // In this case there is nothing to cleanup.
  public void destroyApp(boolean unconditional) { }


  public void commandAction(Command c, Displayable s) {
	  if(c==sendCommand)
		  send(URLField.getString());
	  else if(c==backCommand){
		  URLField.setString(URL);
		  display.setCurrent(mainForm);
	  }
	  else{
          destroyApp(false);
          notifyDestroyed();
	  }
  }

  public void send(String url){
      HttpConnection hpc = null;
      InputStream is = null;

      try {
  	    hpc = (HttpConnection)Connector.open(url);
  	    if(sessionID != null)
  	      hpc.setRequestProperty("cookie",sessionID);
  	    is = hpc.openInputStream();

  	    String cookie = hpc.getHeaderField("Set-cookie");
  	    if(cookie != null){
			int position = cookie.indexOf(';');
			sessionID = cookie.substring(0,position);
		}

		int length = (int)hpc.getLength();
		byte[] response = new byte[length];
		is.read(response);
		String strResponse= new String(response);
		URLField.setString(strResponse);
      }
      catch(Exception e){
		  System.out.println("xxx :error");
	  }
      finally {
		  try{
			 if(is != null) is.close();
             if(hpc != null) hpc.close();
		 }
		 catch(Exception e){
		 }
      }
  }
}

⌨️ 快捷键说明

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