背景
在html语言中,浏览器向服务器发送请求(表单(网页中填写的数据))的时候必须指明请求类型get或post。get把表单打包发送给服务器前,附加到URL(网址)的后面,post把表单打包后隐藏在后台发送给服务器。
区别
1、pos请求提交t比ge请求t提交安全,但是get的执行效率比post好。
因为get请求直接将参数暴露在URL地址中,造成数据泄露;而post请求是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址,地址栏中不显示提交信息。
2、post请求发送的数据量比get请求大。
get请求传送的数据量最大为2KB,post传送的数据量一般被默认为不受限制,但理论上IIS4中最大量为80KB,IIS5中为 100KB。
3、对于get请求,服务器端用context.Request.QueryString
获取变量的值;对于post请求,服务器端用context.Request.Form
获取提交的数据。
Add.html(get)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!--表单:收集用户的数据-->
<form method="get" action="AddInfo.ashx">
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交">
</form>
</body>
</html>
一般处理程序AddInfo.ashx(get)
<%@ WebHandler Language="C#" Class="AddInfo" %>
using System;
using System.Web;
public class AddInfo : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string username = context.Request.QueryString["txtName"];//接收的是表单元素name属性的值
string userpwd = context.Request.QueryString["txtPwd"];
context.Response.Write("用户名是:"+username+",密码是:"+userpwd);
}
public bool IsReusable {
get {
return false;
}
}
}
Add.html(post)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!--表单:收集用户的数据-->
<form method="post" action="AddInfo.ashx">
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交">
</form>
</body>
</html>
一般处理程序AddInfo.ashx(post)
<%@ WebHandler Language="C#" Class="AddInfo" %>
using System;
using System.Web;
public class AddInfo : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string username = context.Request.Form["txtName"];
string userpwd = context.Request.Form["txtPwd"];
context.Response.Write("用户名是:"+username+",密码是:"+userpwd);
}
public bool IsReusable {
get {
return false;
}
}
}
最后,一般情况下,在进行数据的增加、删除、修改等类似机密信息的时候建议大家用post,理由见上述详情;但是在数据的查询过程钟建议大家使用get请求,因为你在进行查询的时候,URL会对应给出你所查询出来的结果,如果你想把你的链接分享给别人, 那么对方也能看到相同的结果。这也有利于网站的推广。但是如果用post进行查询,则打开的并不是最终的结果,我说明白了吗?
注意:
1、在浏览器地址栏中直接输入地址,敲回车,是向服务器发送get请求
2、单击超链接也是get请求
结语
正确的态度既然是一种品质,它就不能靠一时兴起的冲动,更不能急于求成。它应渗透在我们生活的每一点一滴当中,成为我们日常生活的一部分。