快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡亰8455下载app_龟发之家论坛



若何发送一个HTTP POST 哀求

你可以想象,发送一个HTTP POST 哀求的处置惩罚历程着实与发送一个GET 哀求

异常地类似。我们将改动一个现有敕令,添加少量的新的敕令,并添加一个来自

通用连接框架的附加的工具和一个附加的StringBuffer工具把POST哀求体重的内

容发送到办事器中。剩下的敕令将维持不变。

复制我们刚才创建的sendHttpGet ()措施,把它粘贴进同一个类文件,改

名为sendHttpPost()。现在,我们将改动这个新措施来发送一个HTTP POST 请

求到办事器。在措施的顶部添加两个新的变量阐明。声明一个类型为DataOutputStream

的变量和另一个String类型的变量。我们将应用DataOutputStream工具把存在于

字符串变量中的POST哀求体发送到办事器中。

DataOutputStream dos = null ;String requestBody = null ;

改动connector.open()敕令包孕另一个参数,指出连接将容许客户端可以

经由过程连接在办事器上读和写。

hcon =( HttpConnection ) Connector.open ( url,Connector.READ_WRITE )

设置HttpConnection工具应用的哀求措施为POST(默认的措施是G澳门新葡亰8455下载appET )。

hcon.setRequestMethod ( HttpConnection.POST);

获得一个用于现有的HTTP连接的DataOutputStream工具。

dos = hc.openDataOutputStream ();

声明一个字节数组并经由过程检索一个来自requestBody 字符串的字节数组初始

化。然后把DataOutputStream的缓冲写入字节数组内。

byte[] byteRequest = requestBody.getBytes ();

for ( int i = 0;i

//初始化图形用户接口组件

myDisplay= Display.getDisplay( this );

sendCommand

exitCommand = new Command( "EXIT" , Command.OK , 1);

backCommand = new Command( "BACK" ,Command.OK, 1);

//显示哀求的URL

requestScreen = new Form( "Type in a URL :" );

requestField = new TextField ( null , defaultURL , 100,

TextField.URL );

requestScreen.append ( requestField );

requestScreen.addCommand( sendCommand);

requestScreen.addCommand ( exitCommand);

requestScreen.setComma澳门新葡亰8455下载appndListener( this );

//选择想要的HTTP哀求措施

menuItems = new String[] {"GET Request","POST Request"} ;

list = new List( "Select an HTTP method :" ,

List.IMPLICIT,menuItems , null );

list.澳门新葡亰8455下载appsetCommandListener( this );

//先是从办事器上收到的信息

resultScreen = new Form( "Server Response :" );

resultScreen.addCommand( backCommand);

resultScreen.setCommandListener( this );

}// 停止HttpMidlet()

public void startApp() {

myDisplay.setCurrent ( requestScreen );

}//停止 startApp ()

public void commandAction ( Command com,

Displayable disp) {

// 当用户点击"send"按钮

if ( com == sendCommand ){

myDisplay.setCurrent( list );

} else if( com == backCommand ){

requestField.setString( defaultURL );

myDisplay.setCurrent ( requestScreen);

} else if( com == exitCommand ) {

destroyApp ( true );

notifyDestroyed() ;

}//停止 if ( com == sendCommand )

if( disp == list && com ==List.SELECT_COMMAND ){String result;if( list.getSelectedIndex() == 0 ) // 发送一个

GET 哀求到办事器 result = sendHttpGet ( requestField.getString ())

; else //发送一个 POST 哀求到办事器

result = sendHttpPost( requestField.getString());

resultField = new StringItem( null , result );

resultScreen.append( resultField);

myDisplay.setCurrent ( resultScreen );

}//停止if( dis == list && com == List.SELECT_COMMAND)

}//停止 commandAction(Command , Displayable)

private String sendHttpGet( String url )

{

HttpConnection hcon = null;

DataInputStream dis = null ;

StringBu澳门新葡亰8455下载appffer responseMessage = new StringBuffer();

try {

//应用READ权限的标准的 HttpConnection

hcon =( HttpConnection )Connector.open( url);

//从HttpConnection取得一个 DataInputStream

dis = new DataInputStream( hcon.openInputStream());

//从办事器上取回相应

int ch ;

while(( ch = dis.read())!= -1)

{ responseMessage.append ((char) ch );

}//停止while(( ch = dis.read())!= -1)

}

catch( Exception e)

{

e.printStackTrace();

responseMessage.append ( "ERROR");

} finally {

try {

if ( hcon != null) hcon.close ();

if ( dis!= null) dis.close();

} catch( IOException ioe) {

ioe.printStackTrace();

}//停止try/catch

}//停止try/catch/finally

return responseMessage.toString ();

}//停止sendHttpGet( String )

private String sendHttpPost ( String url ) {

HttpConnection hcon = null ;

DataInputStream dis = null ;

DataOutputStream dos = null;

StringBuffer responseMessage = new StringBuffer();

// 哀求体

String requeststring = "This is a POST." ;

try {

//应用读写权限的 HttpConnection

hcon =( HttpConnection )Connector.open( url, Connector.READ_WRITE )

;//设置哀求措施为POST

hcon.setRequestMethod( HttpConnection.POST)

;//取得发送哀求字符串的DataOutputStream

dos = hcon.openDataOutputStream();

byte[] request_body = requeststring.getBytes ();

//发送哀求字 符串到办事器 for( int i = 0; i < request_body.length; i++) {

dos.writeByte( request_body[i]);

}//停止 for( int i = 0; i < request_body.length ; i++)

//取得做为接管办事器相应的DataInputStream

dis = new DataInputStream( hcon.openInputStream ());

//从办事器上取回相应

int澳门新葡亰8455下载app ch ;

while(( ch = dis.read())!= -1) {

responseMessage.append ((char)ch) ;

}//停止while (( ch = dis.read())!= -1) {

}

catch( Exception e )

{ e.printStackTrace();

responseMessage.append ( "ERROR");

}

finally {

//开释输入输出流和HTTP连接

try { i

f ( hcon != null) hcon.close();

if ( dis!= null) dis.close();

if ( dos!= null) dos.close();

} catch( IOException ioe) {

ioe.printStackTrace();

}//停止try/catch }

// 停止try/catch/finally

return responseMessage.toString();

}//停止sendHttpPost( String )

public void pauseApp() {

}//停止pauseApp()

public void destroyApp( boolean unconditional) {

myDisplay= null;

requestScreen = null ;

requestField = null;

resultScreen =null;

resultField = null ;

}//停止 destroyApp ( boolean)

}//停止HttpMidlet

您可能还会对下面的文章感兴趣: