get与post请求
html文件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form method="get" action="Accept.ashx">
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
表单提交到Accept.ashx
<%@ WebHandler Language="C#" Class="Accept" %>
using System;
using System.Web;
public class Accept : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//接收表单提交过来的数据.
string userName=context.Request.QueryString["txtName"];//get请求以QueryString方式接收
string userPwd = context.Request.Form["txtPwd"];//如果是以Post方式提交,那么在服务端接收
//表单数据时需要用到context.Request.Form,并且Form的名字就是表单name属性的值。
context.Response.Write("你输入的用户名是:"+userName+",密码是:"+userPwd);
}
public bool IsReusable {
get {
return false;
}
}
}
以上需注意:
(1)浏览器向服务器发送请求,以什么样的方式发送请求?
get请求:如果表单中的method="get",那么表单中用户输入的数据都是以get方式发送到服务端。所谓的get方式就是将表单中的数据放在了地址栏中,在服务端接收时应该以context.Request.QueryString["txtName"]接收.
post请求:是将表单中的数据放在请求报文体中发送到服务端。并且格式为: txtName(表单元素的name属性的值)=aaa(用户在文本框中输入的值)&txtPwd=aaaa ,如果是以Post方式提交,那么在服务端接收表单数据时需要用到context.Request.Form,并且Form的名字就是表单name属性的值。
(2):请求方式的选择?
如果是提交表单,建议都以post方式提交,因为post方式安全。
如果是提交大数据一定要用post方式。
其它get请求的方式. 在地址栏中直接输入访问的地址。 单击超链接。
没有其它的post请求方式,只有将表单中的method属性该为post.
表单:收集用户的信息
<form method="post" action="Accept.ashx">
用户名:<input type="text" name="txtName" /><br />
密码:<input type="password" name="txtPwd" /><br />
<input type="submit" value="提交" />
</form>
以上表单表示:用户在文本框中输入数据,单击提交按钮,将表单中的数据以post方式,将数据提交到action所指定的动态页面,该页面接收数据并且可以保存到数据库中。