• 002-一般处理程序(HttpHandler)


    一般处理程序(HttpHandler): 是一个实现System.Web.IHttpHandler接口的特殊类。 任何一个实现了IHttpHandler接口的类,是作为一个外部请求的目标程序的前提。(凡是没有实现此接口的类,就不能被浏览器请求。)
    它由支持ASP.NET的服务器调用和启动运行。一个HttpHandler程序负责处理它所对应的一个或一组URL地址的访问请求,并接收客户端发出的访问请求信息(请求报文)和产生响应内容(响应报文)。
    简单的说:可以通过创建一个HttpHandler程序来生成浏览器代码发送回客户端浏览器
    HttpHandler程序可以完成普通类程序所能完成的大多数任务:

    1.获取客户端通过HTML的Form表单提交的数据和URL参数

    2.创建对客户端的响应消息内容

    3.访问服务器端的文件系统

    4.连接数据库并开发基于数据库的应用

    5.调用其他类

    Request(HttpRequest) & Response(HttpResponse)

    Request(HttpRequest)常用成员(服务器如何获取浏览器提交的数据?)

    QueryString属性:获取通过GET方式传来的数据(浏览器:超链接,和表单Method=get)

    //例:context.Request.QueryString["testName"];

    Form属性:获取通过POST方式传来的数据(表单method=post)

    //例:context.Request.Form["testName"];

    Params属性:客户端提交的数据集合

    Response(HttpResponse)常用成员(服务器如何向浏览器响应数据?)

    Write()方法:直接在页面上输出内容

    //例:context.Response.Write("Hello,World!");WriteFile();

    Redirect()方法:重定向到另外一个页面,服务器发送命令让浏览器跳转

    //例:context.Response.Redirect("TypeInfoList.ashx");

    End()方法:结束输出

    HtmlPage1.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <title></title>
     6 </head>
     7 <body>
     8     <h1>Hello World.</h1>
     9 </body>
    10 </html>

    Handler1.ashx

     1     public class Handler1 : IHttpHandler
     2     {
     3 
     4         public void ProcessRequest(HttpContext context)
     5         {
     6             //处理:上文(请求),下文(响应)
     7             //context.Request是HttpRequest类型,包含了所有的请求信息
     8             //context.Response是HttpResponse类型,包含了所有的响应信息
     9 
    10             //告诉浏览器如何去解析执行返回的内容
    11             context.Response.ContentType = "text/plain";
    12 
    13             //返回的信息,通过Write方法直接输出即可
    14             context.Response.Write("<h1>Hello World</h1>");
    15         }
    16 
    17         public bool IsReusable
    18         {
    19             get
    20             {
    21                 return false;
    22             }
    23         }
    24     }

    GetTest.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <title></title>
     6 </head>
     7 <body>
     8     <form method="GET" action="GetTest.ashx">
     9         <input type="text" name="txt2" />
    10         <input type="text" name="txt1" />
    11 
    12         <input type="submit" value="get练习" />
    13     </form>
    14 </body>
    15 </html>

    GetTest.ashx

     1     public class GetTest : IHttpHandler
     2     {
     3 
     4         public void ProcessRequest(HttpContext context)
     5         {
     6             //get请求:name属性指定键,value属性指定值
     7             //以=连接键值对
     8             //再以&符号连接多个请求信息
     9             //再以?符号连接请求地址与参数
    10             //?txt1=123&txt2=456
    11 
    12             //接收参数的方式:以键来接收
    13             string txt1 = context.Request.QueryString["txt1"];
    14             string txt2 = context.Request.QueryString["txt2"];
    15 
    16 
    17             context.Response.ContentType = "text/html";
    18             context.Response.Write(txt1 + "<br/>" + txt2);
    19         }
    20 
    21         public bool IsReusable
    22         {
    23             get
    24             {
    25                 return false;
    26             }
    27         }
    28     }

    PostTest.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <title></title>
     6 </head>
     7 <body>
     8     <form method="POST" action="PostTest.ashx">
     9         <input type="text" name="txt1" />
    10         <input type="text" name="txt2" />
    11         <input type="submit" value="post练习" />
    12     </form>
    13 </body>
    14 </html>

    PostTest.ashx

     1     public class PostTest : IHttpHandler
     2     {
     3 
     4         public void ProcessRequest(HttpContext context)
     5         {
     6             //txt1=123&txt2=456
     7             //请求信息不被拼接在url后面
     8             //以键值对的形式来传递数据,=,&
     9 
    10             //接收以post方式提交的参数
    11             string txt1 = context.Request.Form["txt1"];
    12             string txt2 = context.Request.Form["txt2"];
    13 
    14 
    15             context.Response.ContentType = "text/html";
    16             context.Response.Write(txt1 + "<br/>" + txt2);
    17         }
    18 
    19         public bool IsReusable
    20         {
    21             get
    22             {
    23                 return false;
    24             }
    25         }
    26     }

    GetPostTest.html

     1 <!DOCTYPE html>
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5     <title></title>
     6 </head>
     7 <body>
     8     <form action="GetPostTest.ashx" method="GET">
     9         <input type="text" name="txt1" />
    10         <input type="submit" value="get" />
    11     </form>
    12 
    13     <hr />
    14     <form action="GetPostTest.ashx" method="POST">
    15         <input type="text" name="txt1" />
    16         <input type="submit" value="post" />
    17     </form>
    18 </body>
    19 </html>

    GetPostTest.ashx

     1     public class GetPostTest : IHttpHandler
     2     {
     3 
     4         public void ProcessRequest(HttpContext context)
     5         {
     6             //如果不希望区分请求方式的话,可以直接使用Request索引器
     7             string txt1 = context.Request["txt1"];
     8 
     9             //如果希望将这个值做进一步处理,最好做一个非空判断
    10             //string.IsNullOrEmpty(txt1)
    11 
    12             context.Response.ContentType = "text/plain";
    13             context.Response.Write(txt1);
    14         }
    15 
    16         public bool IsReusable
    17         {
    18             get
    19             {
    20                 return false;
    21             }
    22         }
    23     }

    Web.config

     1 <?xml version="1.0" encoding="utf-8"?>
     2 
     3 <!--
     4   有关如何配置 ASP.NET 应用程序的详细信息,请访问
     5   http://go.microsoft.com/fwlink/?LinkId=169433
     6   -->
     7 
     8 <configuration>
     9   <system.web>
    10     <compilation debug="true" targetFramework="4.5" />
    11     <httpRuntime targetFramework="4.5" />
    12   </system.web>
    13 </configuration>
  • 相关阅读:
    Django中DEBUG模式详解
    Nginx 的负载均衡
    django 上线配置
    vue 项目实现打印
    vue 一键复制,vue-clipboard2的使用方法
    404.vue
    vue-router封装和用户是否需要登录
    uni-app uview 的使用方法
    grid自适应列表
    tomcat的目录分别代表什么含义
  • 原文地址:https://www.cnblogs.com/ninghongkun/p/6268922.html
Copyright © 2020-2023  润新知