標籤:
1. get是從伺服器上擷取資料,post是向伺服器傳送資料。
2. get是把參數資料隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到。post是通過HTTP post機制,將表單內各個欄位與其內容放置在HTML HEADER內一起傳送到ACTION屬性所指的URL地址。使用者看不到這個過程。
3. get傳送的資料量較小,不能大於2KB。post傳送的資料量較大,一般被預設為不受限制。但理論上,IIS4中最大量為80KB,IIS5中為100KB。
4. get安全性非常低,post安全性較高。但是執行效率卻比Post方法好。
建議:
1、get方式的安全性較Post方式要差些,包含機密資訊的話,建議用Post資料提交方式;
2、在做資料查詢時,建議用Get方式;而在做資料添加、修改或刪除時,建議用Post方式;
Request Headers Value
POST:
(Request-Line):POST /wdinfo.php HTTP/1.1
Host:qurl.f.360.cn
Accept:*/*
Cache-Control:no-cache
Content-Type:application/octet-stream
Content-Length:534
返回:回應標頭 值
(Status-Line) HTTP/1.1 200 OK
Cache-Control private
Date Mon, 16 Mar 2015 12:10:18 GMT
Expires Mon, 16 Mar 2015 12:10:18 GMT
Content-type text/html
Server BWS/1.0
Connection Keep-Alive
Content-Length 17
GET:
(Request-Line) GET /?s=20150316200919 HTTP/1.1
Accept image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,*/*
User-Agent Microsoft URL Control - 6.00.8169
Host www.baidu.com
Connection Keep-Alive
Cache-Control no-cache
Cookie BAIDUPSID=0EEE39AFCF8553059F84DD4BD6ED3E55; BAIDUID=C9C6C28B4741A72CB3850803AC4B651E:FG=1; BDUSS=hBY0NWV0tVbGFacGF3Z2lnZW5uQ1ZyZEpxd21xN05VNDBBVDh2R0Z5eDJ0UzFWQVFBQUFBJCQAAAAAAAAAAAEAAAA4B8QcREJaWkNaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHYoBlV2KAZVWU; H_PS_PSSID=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; BDSVRTM=221; BD_HOME=1; BD_UPN=112351; H_PS_BBANNER=5; H_PS_645EC=6439y0Fb0GTIsCg9fss%2FHiFv3rT%2BVoO%2B34IQ6USd5yLUR%2FMAYpol4WMWzhYSRxxEZWOQ
返回:回應標頭 值
(Status-Line) HTTP/1.1 200 OK
Date Mon, 16 Mar 2015 12:09:30 GMT
Content-Type text/html
Connection Keep-Alive
Vary Accept-Encoding
Cache-Control private
Expires Mon, 16 Mar 2015 12:09:30 GMT
Server BWS/1.1
BDPAGETYPE 2
BDQID 0xe7465d0d00000f35
BDUSERID 482608952
Set-Cookie BDSVRTM=193; path=/
Set-Cookie BD_HOME=1; path=/
Set-Cookie H_PS_PSSID=8343_1439_12881_10902_10211_12575_12691_12694_12722_12735_12743_12781_11420_8498; path=/; domain=.baidu.com
Content-Length 148892
一、*************************************************************************HttpPost方式:
// 第1步:建立HttpPost對象
HttpPost httpPost = new HttpPost(url);
// 設定HTTP POST請求參數必須用NameValuePair對象
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName
.getText().toString()));
// 設定HTTP POST請求參數
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 第2步:使用execute方法發送HTTP POST請求,並返回HttpResponse對象
httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
// 第3步:使用getEntity方法獲得返回結果
String result = EntityUtils.toString(httpResponse
.getEntity());
// 去掉返回結果中的“\r”字元,否則會在結果字串後面顯示一個小方格
tvQueryResult.setText(result.replaceAll("\r", ""));
}
****************************************************************************HttpGet方式:
String url = "http://10.197.214.222:8080/querybooks/QueryServlet";
// 向url添加請求參數
url += "?bookname=" + etBookName.getText().toString();
// 第1步:建立HttpGet對象
HttpGet httpGet = new HttpGet(url);
// 第2步:使用execute方法發送HTTP GET請求,並返回HttpResponse對象
httpResponse = new DefaultHttpClient().execute(httpGet);
// 判斷請求響應狀態代碼,狀態代碼為200表示服務端成功響應了用戶端的請求
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第3步:使用getEntity方法獲得返回結果
String result = EntityUtils.toString(httpResponse
.getEntity());
// 去掉返回結果中的“\r”字元,否則會在結果字串後面顯示一個小方格
tvQueryResult.setText(result.replaceAll("\r", ""));
}
二、HttpURLConnection方式:
@Override
public void onFileItemClick(String filename)
{
String uploadUrl = "http://192.168.17.104:8080/upload/UploadServlet";
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
//設定HTTP要求方法,方法名必須大寫,例如:POST,GET
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""
+ filename.substring(filename.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filename);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
dos.close();
is.close();
}
catch (Exception e)
{
setTitle(e.getMessage());
}
}
三、服務端傳回碼:
response.setContentType("text/html;charset=utf-8");
String queryStr = "";
if ("post".equals(request.getMethod().toLowerCase()))
queryStr = "POST請求;查詢字串:" + new String(request.getParameter("bookname").getBytes(
"iso-8859-1"), "utf-8");
else if ("get".equals(request.getMethod().toLowerCase()))
queryStr = "GET請求;查詢字串:" + request.getParameter("bookname");
String s =queryStr
+ "[Java Web開發速學寶典;Java開發指南思想(第4版);Java EE開發寶典;C#開發寶典]";
PrintWriter out = response.getWriter();
out.println(s);
Content-Type對照表:
http://tool.oschina.net/commons
android Get與Post的本質區別