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

📄 passwordmidlet.java

📁 《移动Agent技术》一书的所有章节源代码。
💻 JAVA
字号:
/*
The PasswordMIDlet is located in mobile phone.Being a legal user, people can 
trade stock with Server in this way. We define two forms, you should input a
right user name and the corresponding password in the first form, then the 
second one will be displayed. You can write down the specifically information.
These information will be sent to the Server after the OK button is pressed.

The MIDlet creates a timestamp and a random number,both of which are fed into
the message digest along with the user name and the password.Then the MIDlet
sends the user name, the timestamp, the random mumber, and the digest value up
to the server.
*/
package psw.test;

import java.io.*;
import java.util.Random;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA1Digest;

public class PasswordMIDlet extends MIDlet {
	private Display mDisplay;
	//Define two forms.
	private Form mForm,gForm;
	//Define some text fields.
	private TextField mUserField, mPasswordField;
	private Random mRandom;
	private StringItem mMessageItem;
	TextField number1 = new TextField("股票代码:", "", 10, TextField.ANY);
	TextField number2 = new TextField("股票价格:", "", 10, TextField.ANY);
    TextField number3 = new TextField("股票股数:", "", 10, TextField.NUMERIC);
	//Define two keys.
    static final Command sumCommand = new Command("发送", Command.OK, 2);
    static final Command clearCommand = new Command("清除", Command.STOP, 3);
    //Signals the MIDlet that it has entered the Active state.    
	public void startApp() {
		mDisplay = Display.getDisplay(this);
		mRandom = new Random(System.currentTimeMillis());
	
		if (mForm == null) {
			//Define something in the mForm.
			mForm = new Form("准备登录");
			mUserField = new TextField("用户名","UESTC",32,TextField.ANY);
			mPasswordField = new TextField("密码","uestc",32,TextField.PASSWORD);
			mForm.append(mUserField);
			mForm.append(mPasswordField);
			mForm.addCommand(new Command("退出",Command.EXIT, 0));
			mForm.addCommand(new Command("登录",Command.SCREEN, 0));
			mForm.setCommandListener(new CommandListener() {
				public void commandAction(Command c, Displayable s) {
					if (c.getCommandType() == Command.EXIT) notifyDestroyed();
					else login();
				}
			});
		}
		mDisplay.setCurrent(mForm);
	}

	private void login() {
		//gather the values we'll need.
		long timestamp = System.currentTimeMillis();
		long randomNumber = mRandom.nextLong();
		String user = mUserField.getString();
		byte[] userBytes = user.getBytes();
		byte[] timestampBytes = getBytes(timestamp);
		byte[] randomBytes = getBytes(randomNumber);
		String password = mPasswordField.getString();
		byte[] passwordBytes = password.getBytes();
	
		// Create the message digest.
		Digest digest = new SHA1Digest();
		// Calculate the digest value.
		digest.update(userBytes, 0, userBytes.length);
		digest.update(timestampBytes, 0, timestampBytes.length);
		digest.update(randomBytes, 0, randomBytes.length);
		digest.update(passwordBytes, 0, passwordBytes.length);
		byte[] digestValue = new byte[digest.getDigestSize()];
		digest.doFinal(digestValue, 0);
	
		//Transfer the PasswordServlet lies in the server. The hex encoded 
		//message digest value is included as a parameter.
		URLBuilder ub = new URLBuilder("http://202.115.30.166:8080/servlet/PasswordServlet");
		ub.addParameter("user",user);
		ub.addParameter("timestamp",new String(HexCodec.bytesToHex(timestampBytes)));
		ub.addParameter("random",new String(HexCodec.bytesToHex(randomBytes)));
		ub.addParameter("digest",new String(HexCodec.bytesToHex(digestValue)));
		String url = ub.toString();
			mDisplay = Display.getDisplay(this);
		try {
			// Query the server and retrieve the response.
			HttpConnection hc = (HttpConnection)Connector.open(url);
			InputStream in = hc.openInputStream();
			
			int length = (int)hc.getLength();
			byte[] raw = new byte[1000];
			in.read(raw);
			String response = new String(raw);
			String r="1";     
			System.out.println("re = "+response); 
			System.out.println("response = "+r.compareTo(response));
			int t=r.compareTo(response);
			System.out.println("raw.equals() = "+raw);
			System.out.println("raw="+raw);
            if (t<0){
				//If the user is legal,then let him login in.
				if (gForm==null){
					gForm=new Form("输入股票交易信息");
					gForm.append(number1);
					gForm.append(number2);
					gForm.append(number3);
					gForm.addCommand(clearCommand);
					gForm.addCommand(sumCommand);
 					mDisplay.setCurrent(gForm);
 					gForm.setCommandListener(new CommandListener() {
						public void commandAction(Command c, Displayable s) {
							if (c.getCommandType() == Command.STOP) notifyDestroyed();
							//Transfer the StockServlet if the OK button is pressed.
							else {
								//Using the idea of THREAD, we transfer another Servlet.
								Thread trd =  new Thread() {
									public void run() {
                                       connect();
										}
								};
								trd.start();
 							}
 						}
 					});
 		 		}
 				in.close();
				hc.close();
			}
			else{
				Alert alt = new Alert("警告","输入错误,不能登陆!", null, null);
				alt.setTimeout(Alert.FOREVER);
				mDisplay.setCurrent(alt,mForm);
			}
		}
		catch (IOException ioe) {
			Alert a = new Alert("异常",ioe.toString(), null, null);
			a.setTimeout(Alert.FOREVER);
			mDisplay.setCurrent(a, mForm);
		}
	}

	private byte[] getBytes(long x) {
		byte[] bytes = new byte[8];
		for (int i = 0; i < 8; i++)
			bytes[i] = (byte)(x >> ((7 - i) * 8));
		return bytes;
	}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

private void connect() {
	HttpConnection hc = null;
    InputStream in = null;
     
    URLBuilder ub = new URLBuilder("http://202.115.30.171:8080/servlet/SimpleServlet");
    ub.addParameter("gpdm",number1.getString());
    ub.addParameter("gpjg",number2.getString());
    ub.addParameter("gpgs",number3.getString());
    String ur = ub.toString();
    try {
		hc = (HttpConnection)Connector.open(ur);
		in = hc.openInputStream();

		int contentLength = (int)hc.getLength();
		byte[] raw = new byte[contentLength];
		int length = in.read(raw);
		
		String response = new String(raw);
		Alert a = new Alert("异常",response, null, null);
		a.setTimeout(Alert.FOREVER);
		mDisplay.setCurrent(a,mForm);
       
		in.close();
		hc.close();
    }
   
    catch (IOException ioe) {
    mMessageItem.setText(ioe.toString());
    }
    mDisplay.setCurrent(gForm);
}
}

⌨️ 快捷键说明

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