表單提交中get和post方式的區別:
1.get是從服務器上獲取數據,post是向服務器傳送數據。
2.get是把參數數據隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個字段一一對應,在URL中可以看到。post是通過HTTPpost機制,將表單內各個字段與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。用戶看不到這個過程。
3.對於get方式,服務器端用Request.QueryString獲取變量的值,對於post方式,服務器端用Request.Form獲取提交的數據。
4.get 傳送的數據量較小,不能大於2KB。post傳送的數據量較大,一般被默認爲不受限制。但理論上,IIS4中最大量爲80KB,IIS5中爲100KB。
5.get安全性非常低,post安全性較高。
那麼接下來讓我們看看在Android平臺開發中如何執行一個Post請求:
Java代碼:
String uriAPI = "http://192.168.1.100:8080/hwhw/hwhw.jsp"; //這是我測試的本地,大家可以隨意改
HttpPost httpRequest = new HttpPost(uriAPI);
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("u", "呵呵噠));
params.add(new BasicNameValuePair("p", "1234"));
try
{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}