httpConnection 里的 setRequestProperty怎么用啊,我想通过手机客户端链接到服务器,并且在客户端输入关键字查询信息,在服务器那边返回查询结果给客户端这个setRequestProperty请求是在页面上获得吗?比如我要通过页面来验证登陆的话,
就用setRequestProperty("username",username);
setRequestProperty("password",password);谢了,但是我还想问下setRequestProperty是做什么用的,是设置请求链接头?里面的参数又是什么呢?void setRequestProperty(String key, String value)
Sets the general request property
设置一般的请求属性,如果一个属性的关键字存在了,将用新设置的值覆盖原来的值。
看我上面的例子里面是设置文本内容的格式的加上客户端希望使用无格式的文本内容类型和GET方法请求应答,MIDlet应该包含下面代码:
HttpConnection hc=(HttpConnection)Connector.open(url);
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("Content-type","//text/plain");
手机客户端链接到服务器,并且在客户端输入关键字查询信息,在服务器那边返回查询结果给客户端:
import java.io.DataInputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
//送给服务器一些信息,得到响应,例如:
public class MIDlet3 extends MIDlet implements CommandListener{
private Form frm = new Form("HTTP测试");
private TextField tfAcc = new TextField("输入账号","",10,TextField.ANY);
private TextField tfPass = new TextField("输入账号","",10,TextField.ANY);
private Command cmdLogin = new Command("登陆",Command.SCREEN,1);
private Display dis;
protected void startApp() throws MIDletStateChangeException {
dis = Display.getDisplay(this);
dis.setCurrent(frm);
frm.append(tfAcc);
frm.append(tfPass);
frm.addCommand(cmdLogin);
frm.setCommandListener(this);
}
public void commandAction(Command c,Displayable d){
if(c==cmdLogin){
ValidateTehread vt = new ValidateTehread();
vt.start();
}
}
class ValidateTehread extends Thread{
public void run(){
try{
String url = "http://localhost:9999/login.jsp?acc="+tfAcc.getString()+"&pass="+tfPass.getString();
//连接到HTTP服务器
HttpConnection hc =
(HttpConnection)Connector.open(url);
DataInputStream dis = hc.openDataInputStream();
byte[] b = new byte[(int)hc.getLength()];
dis.read(b);
frm.append(new String(b));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
}