Http Get 和 Post
最近工作中要求由客户端向服务端发送数据,采用的是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安全性高,传输数据量大。以此作为笔记,如有不足之处,请指正。