一般处理程序(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

 <!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>
<h1>Hello World.</h1>
</body>
</html>

Handler1.ashx

     public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//处理:上文(请求),下文(响应)
//context.Request是HttpRequest类型,包含了所有的请求信息
//context.Response是HttpResponse类型,包含了所有的响应信息 //告诉浏览器如何去解析执行返回的内容
context.Response.ContentType = "text/plain"; //返回的信息,通过Write方法直接输出即可
context.Response.Write("<h1>Hello World</h1>");
} public bool IsReusable
{
get
{
return false;
}
}
}

GetTest.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="GetTest.ashx">
<input type="text" name="txt2" />
<input type="text" name="txt1" /> <input type="submit" value="get练习" />
</form>
</body>
</html>

GetTest.ashx

     public class GetTest : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//get请求:name属性指定键,value属性指定值
//以=连接键值对
//再以&符号连接多个请求信息
//再以?符号连接请求地址与参数
//?txt1=123&txt2=456 //接收参数的方式:以键来接收
string txt1 = context.Request.QueryString["txt1"];
string txt2 = context.Request.QueryString["txt2"]; context.Response.ContentType = "text/html";
context.Response.Write(txt1 + "<br/>" + txt2);
} public bool IsReusable
{
get
{
return false;
}
}
}

PostTest.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="PostTest.ashx">
<input type="text" name="txt1" />
<input type="text" name="txt2" />
<input type="submit" value="post练习" />
</form>
</body>
</html>

PostTest.ashx

     public class PostTest : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//txt1=123&txt2=456
//请求信息不被拼接在url后面
//以键值对的形式来传递数据,=,& //接收以post方式提交的参数
string txt1 = context.Request.Form["txt1"];
string txt2 = context.Request.Form["txt2"]; context.Response.ContentType = "text/html";
context.Response.Write(txt1 + "<br/>" + txt2);
} public bool IsReusable
{
get
{
return false;
}
}
}

GetPostTest.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 action="GetPostTest.ashx" method="GET">
<input type="text" name="txt1" />
<input type="submit" value="get" />
</form> <hr />
<form action="GetPostTest.ashx" method="POST">
<input type="text" name="txt1" />
<input type="submit" value="post" />
</form>
</body>
</html>

GetPostTest.ashx

     public class GetPostTest : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
//如果不希望区分请求方式的话,可以直接使用Request索引器
string txt1 = context.Request["txt1"]; //如果希望将这个值做进一步处理,最好做一个非空判断
//string.IsNullOrEmpty(txt1) context.Response.ContentType = "text/plain";
context.Response.Write(txt1);
} public bool IsReusable
{
get
{
return false;
}
}
}

Web.config

 <?xml version="1.0" encoding="utf-8"?>

 <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>

002-一般处理程序(HttpHandler)的更多相关文章

  1. 一般处理程序HttpHandler的应用

    ashx 一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名.一个httpHandler接受并处理一个http请求,类比于Java中的servlet.类比于在Ja ...

  2. 使用一般处理程序HTTPHandler下载文件

    一般来说我们可以用HTTPHandler来处理一些简单的逻辑,比如验证码.下载文件等. 以下载word文档为例讲解一下如何在HHTPHandler中下载文件,不限于word文档,如果下载其他文件,需要 ...

  3. EasyUI - 使用一般处理程序 HttpHandler (.ashx)

    以easyui中的panel中,使用url加载数据为列. 效果: html代码: <div id="p" style="padding: 10px;"&g ...

  4. 【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...

  5. HttpModule,HttpHandler,HttpHandlerFactory

    HttpModule:Http模块,可以在页面处理前后.应用程序初始化.出错等时候加入自己的事件处理程序. HttpHandler:Http处理程序,处理页面请求 HttpHandlerFactory ...

  6. .net-一般处理程序及生命周期

    IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...

  7. 【.net项目中。。】.net一般处理程序使用方法

    1.基本原理图 IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHa ...

  8. C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用

    这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用.HttpModule:Http模块,可以在页面处理前后.应用 ...

  9. asp.net中的路由系统

    ASP.NET MVC重写了ASP.NET管道HttpModule和处理程序HttpHandler.MVC自定义了MvcHandler实现了Controller的激活和Action的执行.但是在请求到 ...

随机推荐

  1. 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题

    https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...

  2. Oracle分析函数大全

    分析函数又叫开窗函数,OLAP函数等,因为有人问我用过开窗函数没,呵,什么是开窗函数,从来没听过,难道是分析函数么.哈哈,最后还真是分析函数哦!用过的东西别名也应该知道,赶上这么个事,就剽窃一眼Ora ...

  3. typeof(), __typeof(), __typeof__(), -isKindOfClass:的区别

    关于  typeof()和 __typeof()  和 __typeof__() ,Stack Overflow 有一段解释,大概意思是, __typeof() .__typeof__() 是C语言的 ...

  4. 新浪广告交易平台(SAX)DSP手册

    新浪广告交易平台(SAX)DSP手册 http://amp.ad.sina.com.cn/sax/doc/zh-CN/xhtml/index.xhtml 新浪广告交易平台(SAX)DSP手册 版权 © ...

  5. Xcode工程编译错误:“Cannot assign to 'self' outside of a method in the init family”

    #import <Foundation/Foundation.h> @interface EOCRectangle : NSObject<NSCoding> @property ...

  6. 如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)

    //宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...

  7. 【Python基础】random 的高级玩法

    random 模块的高级玩法 1.python 随机产生姓名 方式一: import random xing = [ '赵', '钱', '孙', '李', '周', '吴', '郑', '王', ' ...

  8. 利用soapui测试http接口(参数化+关联)

      一.建立新工程 二.建立测试套件   三.新建用例     四.建立http请求       五.写路径该编码格式 写在这里是因为在请求路径写会把大写的路径默认改为小写 六.参数化.关联   七. ...

  9. JavaScript学习笔记--语言工具的了解

    基础学习,快速入门资料:网站 https://www.liaoxuefeng.com ,http://www.runoob.com/js/js-tutorial.html 笔记: 编程工具:SubLi ...

  10. mv 命令

    [root@localhost soft]# .txt [root@localhost soft]# [root@localhost soft]# ls .txt [root@localhost so ...