该请求方法是默认方法,所以如果你不使用setRequestMethod来设置的话,就是说你使用GET方法,下面我们给出一个代码片段来演示GET方法的具体使用。
HttpConnection c;
InputStream is;
Try{
C=(HttpConnection)Connector.open(“http://java.sun.com/”);
//判断连接是否正常
int status=c.getResponseCode();
if(status!=HttpConnection.HTTP_OK)
throw new IOException(“Response code not ok”);
else{
is=c.openInputStream();//打开输入流,读入数据
int ch;
while((ch=is.read())!=-1){
//处理数据
}
}
}finally{
if(is!=null)
is.close();
if(c!=null)
c.close();
}
POST
POST请求比GET请求可以发送更多的参数到服务器端,使用POST请求的时候可以在URL的一部分中添加参数(同GET),同时也可以使用一个流对象把参数传递给服务器。
下面的代码片段演示了使用流传递参数
HttpConnection c;
InputStream is;
OutputStream os;
Try{