Http Get 和 Post
最近工作中要求由客戶端向服務(wù)端發(fā)送數(shù)據(jù),采用的是Http協(xié)議,即Get和Post請(qǐng)求操作。通常情況下,Get用于請(qǐng)求數(shù)據(jù),Post用于上傳數(shù)據(jù)更新服務(wù)器。Get請(qǐng)求應(yīng)該是安全的和等冪的。
在提交表單時(shí),如果將表單的Method屬性設(shè)置為get,那么表單提交采用get方式提交數(shù)據(jù)發(fā)送請(qǐng)求,使用get方式,表單中的信息是以Key=Value&Key=Value的方式連接在Url之后。采用ASP.NET MVC 3舉例驗(yàn)證:
View:
@{ViewBag.Title = "Http Get 與 Post學(xué)習(xí)分析"; }<h2>Http Get 與 Post學(xué)習(xí)分析</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();}}初始運(yùn)行:
UserName、Password文本框分別輸入:123456,單擊Submit按鈕:
該get請(qǐng)求Headers:
Request URL:http://localhost:4126/?userName=123456&passWord=123456 Request Method:GET Status Code:200 OKRequest 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.19Query String Parameters,連接到Url之后
userName:123456 passWord:123456Response 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方式,數(shù)據(jù)存放在Url中的,采用明文方式,所以安全性不高,并且get方式最多只能傳輸1024個(gè)字節(jié)
對(duì)于Post方式,表單中的數(shù)據(jù)時(shí)存放在http請(qǐng)求的Header中的,不可見(jiàn)。
View:
@{ViewBag.Title = "Http Get 與 Post學(xué)習(xí)分析"; }<h2>Http Get 與 Post學(xué)習(xí)分析</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();}}初始運(yùn)行:
UserName、Password文本框分別輸入:123456,單擊Submit按鈕:
下面來(lái)觀察http Header:
Request URL:http://localhost:4126/ Request Method:POST Status Code:200 OKRequest 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.19Form Data:post方式數(shù)據(jù)存放容器
userName:123456 passWord:123456Response 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由此可見(jiàn):Post安全性高,傳輸數(shù)據(jù)量大。以此作為筆記,如有不足之處,請(qǐng)指正。
轉(zhuǎn)載于:https://www.cnblogs.com/PerfectSoft/archive/2012/05/21/2511043.html
總結(jié)
以上是生活随笔為你收集整理的Http Get 和 Post的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式十三:proxy(代理)——对象
- 下一篇: 近况