客户端浏览器 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="post" action="Handler.ashx">-->
<form method="get" action="Handler.ashx">
用户名:<input type="text" name="txt" /><br />
密码:<input type="password" name="pwd" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
服务器端一般处理程序代码:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
context.Response.ContentType = "text/html";
// 如果表单是以 POST 方式提交的,则服务器端必须以 Request.Form[name] 来获取;
// 表单元素必须要有 name 属性,因为Form[] 中的索引就是 name 属性的值;
//string userName = context.Request.Form["txt"];
//string userPwd = context.Request.Form["pwd"];
// 如果表单元素以 GET 方式提交,则服务器端必须以 Request.QueryString[] 来获取,索引仍是name属性的值。
// 以 GET 方式提交的时候,会在浏览器的地址栏显示提交的内容。
string userName = context.Request.QueryString["txt"];
string userPwd = context.Request.QueryString["pwd"];
// 将信息输出到客户端浏览器
context.Response.Write("用户名:" + userName + "<br/>密码:" + userPwd);
}
public bool IsReusable
{
get
{
return false;
}
}
}
在浏览器上打开html页面,请求信息:
点击提交之后的响应信息: