一般处理程序获取客户端post和get的请求信息

客户端浏览器 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页面,请求信息:


点击提交之后的响应信息:


  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值