Sunday, July 13, 2008

HTTP Handlers in .Net

Introduction :
In simple words what i understand is that as the name suggests "they are meant to process the http Request by the client and generate the response. "

They are uesd by ASP.Net to handle requests for the .aspx pages.

Why and Where to use:

use of HTTP Handler(your custom) can be a alternative and better approach than .aspx page
depending on the scenario
for example :
application to application Interaction ( data posted by using XML Http from one form to other and receiving response)

Dim requestXML As New StringBuilder()
Dim oXMLHttp As New XMLHTTP

requestXML.Append("")
requestXML.Append("Rate")
requestXML.Append("Shop")

Dim Domain As String = HttpContext.Current.Request.ServerVariables("Server_Name") + ":" + HttpContext.Current.Request.ServerVariables("Server_Port")

oXMLHttp.open("POST", "http://" + Domain + "/httphandling/Default.aspx", False)
oXMLHttp.setRequestHeader("Content-Length", (requestXML.ToString.Length))
oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
oXMLHttp.send(requestXML.ToString)
Dim response As String = oXMLHttp.responseText

In the above code a xml string is sent to the "Default.aspx" where it is processed and Response is sent back.
So, what is the role of Default.aspx more specifically no visual content.

this is the case where there is Over head of Processing of the .Aspx page life cycle

So, Http Handler Can be used to overcome this Issue.


How to use:

There are Two Approaches Available to my knowledge.

The one require more effort the other bit easier.

First Approach :

Implementing The System.Web.IHttpHandler Details

I personally Found The Following Article Very Helpful Thanks to the author
Implementing HttpHandelers

Now Let us come The :

Second Approach:

Using The .ashx "web Handler" the template is available in Visual studio.Net
you donot require registring the file extension in web.Config

for details : visit

Now using This approach:

on The page sending Request:


Dim requestXML As New StringBuilder()
Dim oXMLHttp As New XMLHTTP
requestXML.Append("")
requestXML.Append("Rate")
requestXML.Append("Shop")

Dim Domain As String = HttpContext.Current.Request.ServerVariables("Server_Name") + ":" + HttpContext.Current.Request.ServerVariables("Server_Port")

oXMLHttp.open("POST", "http://" + Domain + "/httphandling/Handler.ashx", False)
oXMLHttp.setRequestHeader("Content-Length", (requestXML.ToString.Length))
oXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
oXMLHttp.send(requestXML.ToString)
Dim response As String = oXMLHttp.responseText


On The Handler.ashx


<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.IO;


public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/html";
StreamReader rd = new StreamReader(context.Request.InputStream);

string str=rd.ReadToEnd();
//processing of the request string
context.Response.Write("Yes, I received you request");

}

public bool IsReusable {
get {
return false;
}
}

}


I hope this might be helpful to the readers