首页  :: 订阅 订阅  :: 管理

使用IHTTPHandler抓取http context

Posted on 2005-04-22 10:30  GaoJie[高杰]  阅读(385)  评论(0编辑  收藏  举报
Enables processing of HTTP Web requests by a custom HttpHandler that implements the IHttpHandler interface.

Parameters

context
An HttpContext object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.


Example

The following example writes four lines of text to the HTTP output stream in response to a client request for a page named handler.aspx. All requests for handler.aspx are serviced by the MyHttpHandler class in the namespace HandlerExample contained in the assembly HandlerTest.dll.


'______________________________________________________________
'
' To use this handler, include the following lines in a
' Web.config file (be sure to remove the comment markers).
'
'<configuration>
'   <system.web>
'      <httpHandlers>
'         <add verb="*" path="handler.aspx" type="HandlerTest.HandlerExample.MyHttpHandler,HandlerTest"/>
'      </httpHandlers>
'   </system.web>
'</configuration>

[C#] 
// Name this C# file HandlerTest.cs and compile it with the
// command line: csc /t:Library /r:System.Web.dll HandlerTest.cs.
// Copy HandlerTest.dll to your \bin directory.

using System.Web;

namespace HandlerExample
{
   public class MyHttpHandler : IHttpHandler
   {
      // Override the ProcessRequest method.
      public void ProcessRequest(HttpContext context)
      {
         context.Response.Write("<H1>This is an HttpHandler Test.</H1>");      
         context.Response.Write("<p>Your Browser:</p>");
         context.Response.Write("Type: " + context.Request.Browser.Type + "<br>");
         context.Response.Write("Version: " + context.Request.Browser.Version);
      }

      // Override the IsReusable property.
      public bool IsReusable
      {
         get { return true; }
      }
   }
}