文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Login to HTTP Server from a J2ME Program

Login to HTTP Server from a J2ME Program

时间:2010-04-01  来源:admin126com

Login to HTTP Server from a J2ME Program

This J2ME sample program shows how to display a simple LOGIN SCREEN on the J2ME phone and how to authenticate to a HTTP server.

Many J2ME applications for security reasons require the authentication of the user. This free J2ME sample program, shows how a J2ME application can do authentication to the backend server.

/*
 * A free J2ME sample program to demonstrate
 * a SIMPLE LOGIN SCREEN TO LOGIN TO A HTTP SERVER 
 * FROM A J2ME phone
 *
 * @author William Alexander
 * free for use as long as this comment is included in
 * in the program as it is
 * More Free Java programs available for download 
 * at http://www.java-samples.com
 *
 */
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;  
import java.io.*;
import java.lang.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;
import  com.symbol.j2me.midlets.ascanner.BarCodeReader;
public class Login extends MIDlet implements CommandListener {
    
        TextField UserName=null;
        TextField Location=null;
        Form authForm,mainscreen;
        TextBox t = null;
        StringBuffer b = new StringBuffer();
    private Display myDisplay = null;
    private Command okCommand = new Command("OK", Command.OK, 1);
    private Command exitCommand = new Command("Exit", Command.EXIT, 2);
    private Command backCommand = new Command("Back", Command.BACK, 2);
    private Alert alert = null;

    public Login() {        

        myDisplay = Display.getDisplay(this);

        UserName=new TextField("Your Name","",10,TextField.ANY);
        Location=new TextField("Location","",10,TextField.ANY);
        authForm=new Form("Identification");
        mainscreen=new Form("Logging IN");
        mainscreen.append("Logging in....");
        mainscreen.addCommand(backCommand);
        authForm.append(UserName);
        authForm.append(Location);
        authForm.addCommand(okCommand);
        authForm.addCommand(exitCommand);
        authForm.setCommandListener(this);
        myDisplay.setCurrent(authForm);

    }

    public void startApp() throws MIDletStateChangeException { 
    }

    public void pauseApp() {
    }


  
    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {
    }

  
    public void commandAction(Command c, Displayable d) {
        
        if ((c == okCommand) && (d == authForm)) {
        if (UserName.getString().equals("") || Location.getString().equals("")){
                alert = new Alert("Error", "You should enter Username and Location", null, AlertType.ERROR);
                alert.setTimeout(Alert.FOREVER);
                myDisplay.setCurrent(alert);
        }
        else{
        //myDisplay.setCurrent(mainscreen);
        login(UserName.getString(),Location.getString());
        }
        }
        if ((c == backCommand) && (d == mainscreen)) {
                myDisplay.setCurrent(authForm);
        }
        if ((c == exitCommand) && (d == authForm)) {
                notifyDestroyed();
        }
        }
        
        public void login(String UserName,String PassWord) {
        HttpConnection connection=null;
        DataInputStream in=null;
        String url="http://www.java-samples.com/use-your-own/urlhere.jsp";

        OutputStream out=null;  
        try
        {
                connection=(HttpConnection)Connector.open(url);
                connection.setRequestMethod(HttpConnection.POST);
                connection.setRequestProperty("IF-Modified-Since", "2 Oct 2002 15:10:15 GMT");
                connection.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
                connection.setRequestProperty("Content-Language", "en-CA");
                connection.setRequestProperty("Content-Length",""+ (UserName.length()+PassWord.length()));
                connection.setRequestProperty("UserName",UserName);
                connection.setRequestProperty("PassWord",PassWord);
        out = connection.openDataOutputStream();
        out.flush();
        in = connection.openDataInputStream();
        int ch;
        while((ch = in.read()) != -1) {
                b.append((char) ch);
                //System.out.println((char)ch);
        }
        //t = new TextBox("Reply",b.toString(),1024,0);
        mainscreen.append(b.toString());
        if(in!=null)
                in.close();
        if(out!=null)
                out.close();
        if(connection!=null)
                connection.close();
        } 
        catch(IOException x){}
        myDisplay.setCurrent(mainscreen);
        
    }
} 
相关阅读 更多 +
排行榜 更多 +
《InZOI》休闲项圈MOD

《InZOI》休闲项圈MOD

行业软件 下载
《剑星》伊芙的性感蕾丝紧身胸衣MOD

《剑星》伊芙的性感蕾丝紧身胸衣MOD

行业软件 下载
《上古卷轴5:天际重制版》钢羽纸莎草延伸MOD

《上古卷轴5:天际重制版》钢羽纸莎草延伸MOD

行业软件 下载