最近工作中要求由客戶端向服務端發送數據,采用的是Http協議,即Get和Post請求操作。通常情況下,Get用於請求數據,Post用於上傳數據更新服務器。Get請求應該是安全的和等冪的。
在提交表單時,如果將表單的Method屬性設置為get,那么表單提交采用get方式提交數據發送請求,使用get方式,表單中的信息是以Key=Value&Key=Value的方式連接在Url之后。采用ASP.NET MVC 3舉例驗證:
View:
@{
ViewBag.Title = "Http Get 與 Post學習分析";
}
<h2>Http Get 與 Post學習分析</h2>
@using (Html.BeginForm("Add", "Student", FormMethod.Get))
{
<table>
<tr>
<td>UserName:</td>
<td><input type="text" name="userName" id="userName" /></td>
<td>Password:</td>
<td><input type="text" name="passWord" id="password" /></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
Controller:
public class StudentController : Controller { public ActionResult Add(string userName, string passWord) { string information = "UserName: " + userName + "Password: " + passWord; return View(); } }
初始運行:
UserName、Password文本框分別輸入:123456,單擊Submit按鈕:
該get請求Headers:
Request URL:http://localhost:4126/?userName=123456&passWord=123456 Request Method:GET Status Code:200 OK
Request Headers:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:zh-CN,zh;q=0.8 Connection:keep-alive Host:localhost:4126 Referer:http://localhost:4126/ User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
Query String Parameters,連接到Url之后
userName:123456 passWord:123456
Response Headers:
Cache-Control:private Connection:Close Content-Length:408 Content-Type:text/html; charset=utf-8 Date:Sun, 20 May 2012 16:11:00 GMT Server:ASP.NET Development Server/10.0.0.0 X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:3.0
由於get方式,數據存放在Url中的,采用明文方式,所以安全性不高,並且get方式最多只能傳輸1024個字節
對於Post方式,表單中的數據時存放在http請求的Header中的,不可見。
View:
@{
ViewBag.Title = "Http Get 與 Post學習分析";
}
<h2>Http Get 與 Post學習分析</h2>
@using (Html.BeginForm("Add", "Student", FormMethod.Post))
{
<table>
<tr>
<td>UserName:</td>
<td><input type="text" name="userName" id="userName" /></td>
<td>Password:</td>
<td><input type="text" name="passWord" id="password" /></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
}
Controller:
public class StudentController : Controller { public ActionResult Add() { return View(); } [HttpPost] public ActionResult Add(string userName, string passWord) { string information = "UserName: " + userName + "Password: " + passWord; return View(); } }
初始運行:
UserName、Password文本框分別輸入:123456,單擊Submit按鈕:
下面來觀察http Header:
Request URL:http://localhost:4126/
Request Method:POST
Status Code:200 OK
Request Headers:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3 Accept-Encoding:gzip,deflate,sdch Accept-Language:zh-CN,zh;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:31 Content-Type:application/x-www-form-urlencoded Host:localhost:4126 Origin:http://localhost:4126 Referer:http://localhost:4126/ User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
Form Data:post方式數據存放容器
userName:123456 passWord:123456
Response Headers:
Cache-Control:private Connection:Close Content-Length:409 Content-Type:text/html; charset=utf-8 Date:Sun, 20 May 2012 16:27:55 GMT Server:ASP.NET Development Server/10.0.0.0 X-AspNet-Version:4.0.30319 X-AspNetMvc-Version:3.0
由此可見:Post安全性高,傳輸數據量大。以此作為筆記,如有不足之處,請指正。