Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process for ASP.NET to process HTTP requests

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the process of ASP.NET processing HTTP requests". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the process of ASP.NET processing HTTP requests" can help you solve the problem.

1. ASP.NET processing pipeline

The first step in the Asp.net processing pipeline is to create a HttpWorkerRequest object that contains all the information about the current request.

HttpWorkerRequest passes the request to the static ProcessRequest method of the HttpRuntime class. The first thing HttpRuntime needs to do is to create a HttpContext object and initialize it with HttpWorkerRequest.

After the HttpContext instance is created, the HttpRuntime class requests an example of the HttpApplication derived class for the application by calling the static GetApplicationInstance () method of HttpApplicationFactory. The GetApplicationInstance () method either creates a new instance of the HttpApplication class or fetches an instance from the application object pool.

After the HttpApplication instance is created, it is initialized and the application-defined places are allocated during initialization

There's a module. The modular class that implements the IHttpModule interface is designed to implement the classic 19 standard handling events.

After the module is created, the HttpRuntime class requires the newly retrieved HttpApplication class to service the current request by calling its BeginProcessRequest method. Then, find the appropriate handler factory for the current request.

Create a handler, pass the current HttpContext, and once the ProcessRequest method returns, the request is completed.

II. IHttpHandler

HttpHandler is where asp.net really handles Http requests. In this HttpHandler container, ASP.NET Framework actually compiles and executes the server page requested by the client, and appends the processed information to the HTTP request information flow and returns it to the HttpModule.

When a HTTP request is passed to the HttpHandler container through the HttpModule container, ASP.NET Framework calls the ProcessRequest member method of HttpHandler to actually process the HTTP request. And the result of processing continues to be passed through HttpModule until it reaches the client.

HttpHandler is different from HttpModule in that once its own HttpHandler class is defined, its relationship to the HttpHandler of the system will be "overridden".

Application 1: image hotlink protection (implement a custom IHttpHandler)

First: define a class that implements IHttpHandler and implement its ProcessRequest method. If you need to access Session in a HttpHandler container, you must implement the IRequiresSessionState interface, which is just a tagged interface without any method.

Public class PictureHttpHandler: IHttpHandler {public bool IsReusable {get {return true;}} public void ProcessRequest (HttpContext context) {/ / site domain name string myDomain = "localhost"; if (context.Request.UrlReferrer = = null | | context.Request.UrlReferrer.Host.ToLower () .IndexOf (myDomain))

< 0) { //如果是通过浏览器直接访问或者是通过其他站点访问过来的,则显示"资源不存在"图片 context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(context.Request.PhysicalApplicationPath + "/images/noimg.jpg"); } else { //如果是通过站内访问的,这正常显示图片 context.Response.ContentType = "image/JPEG"; context.Response.WriteFile(context.Request.PhysicalPath); } }} 第二:在web.config中注册这个类,并且指定Handler处理的请求类型,把此节点插入system.web节点中 正常访问default页面时: 通过图片地址直接访问时: 应用2、生成验证码public class ValidateCodeHttpHandler : IHttpHandler{ public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/gif"; //建立Bitmap对象,绘图 Bitmap basemap = new Bitmap(200, 60); Graphics graph = Graphics.FromImage(basemap); graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60); Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel); Random r = new Random(); string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ"; string letter; StringBuilder s = new StringBuilder(); //添加随机的五个字母 for (int x = 0; x < 5; x++) { letter = letters.Substring(r.Next(0, letters.Length - 1), 1); s.Append(letter); graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15)); } //混淆背景 Pen linePen = new Pen(new SolidBrush(Color.Black), 2); for (int x = 0; x < 6; x++) graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59))); //将图片保存到输出流中 basemap.Save(context.Response.OutputStream, ImageFormat.Gif); //context.Session["CheckCode"] = s.ToString(); //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片 context.Response.End(); } public bool IsReusable { get { return true; } }} 把下面的项加到web.config中的httphandler节点中: 访问validatevode时: 三、自定义HttpModule: 每次请求的开始和结束定义的HttpModule。 在Asp.net中,创建在System.Web命名空间下的IHttpModule接口专门用来定义HttpApplication对象的事件处理。实现IHttpModule接口的类称为HttpModule(Http模块)。 1、通过IHttpModule创建HttpApplication的事件处理程序public class ModuleExample : IHttpModule{ public void Init(System.Web.HttpApplication application) { application.PostAuthenticateRequest += (sender, args) =>

{HttpContext context = ((HttpApplication) sender) .context; context.Response.Write ("request PostAuthenticate");}; application.BeginRequest + = (sender, args) = > {HttpContext context = ((HttpApplication) sender) .context; context.Response.Write ("request arrives");} Application.EndRequest + = (sender, args) = > {HttpContext context = ((HttpApplication) sender) .Context; context.Response.Write ("request end");};} public void Dispose () {throw new NotImplementedException ();}} 2, register HttpModule

In Asp.net, implementing the IHttpModule interface is only the first step in implementing HttpModule, and the HttpModule used in Asp.net must also be registered in the website configuration file to really take effect and be used in Asp.net.

3. Common HttpModule

In Asp.net, many HttpModule have been predefined and even registered in the server's website configuration file. In the system folder C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ Config\ web.config, you can see that the registered HttpModule is as follows:

This is the end of the content about "what is the process of ASP.NET processing HTTP requests". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report