ASHX HTTP Handler to read online website in ASP.NET

Friday, October 30, 2009 |



SQLHub.com basically focuses on SQL Server and related technology even sometime when I feel something in .NET very special, I would like to share with my blog readers.

Generally in ASP.NET we used to work with .ASPX file most. ASPX file is heart of ASP.NET technology. Basically ASPX file represent HTML kind of representation which used to bind with ASPX.CS file which is largely known as code-behind file. When you run ASPX file, it compiles code-behind and display the results but have you ever think that you could bypass code-behind file?

ASXH (HTTP Handler) is really very powerful but underutilized utility. Even if you will search GOOGLE, you won’t find much example. I tried to GOOGLE this topic and found mostly one example of retrieving images with HTTP Handler.  HTTP Handler is not limited to retrieve images only but it is really VERY powerful tool to use.

I thought to present different example of HTTP Handler so I prepared one small demo of it which read the URL given in that.

NOTE: this is just a very BASIC demo; you can customize the code and can use it for your own purpose. It has lots of scope to improvement but the intention of this article is to show you different usage of HTTP Handler only.

Please follow the below given steps to make one sample example.
1.)    Create new project in VS 2008, steps are: File->New->Project->Visual C#->Asp.Net web application

2.)    Right click on website name in solution explorer and click one Add->New Item->Generic Handler

3.)    Give the name “Handler.ashx” to the file and click on “Add” button.

4.)    Copy and paste the below given code in your Handler.ashx.cs file. (You should already have Default.aspx file in your website but we don’t want to add anything in that, rather we will execute that page only when we will finish code)

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        const string ROOT = "http://www.SQLHub.com";

        string url = context.Request.QueryString.ToString();

        CookieContainer CC = new CookieContainer();

        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(ROOT + HttpUtility.UrlDecode(url));
        Req.CookieContainer = CC;

        try
        {
            WebResponse webResponse = Req.GetResponse();
            string sTxt = new System.IO.StreamReader(webResponse.GetResponseStream(),
                System.Text.Encoding.UTF8).ReadToEnd();
            webResponse.Close();

            context.Response.ContentType = webResponse.ContentType;
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(sTxt);
        }
        catch
        {
            context.Response.Redirect(url);
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

5.)    Go to your web.config file, add following one line under Configuration->System.Web->httpHandler

<add verb="*" path="Default.aspx" type="Handler"/>

6.)    That’s it; you are now ready to run your application without even writing single line of code in your default.aspx. Open your Default.aspx page by double clicking on it from solution explorer and run your website by pressing F5 key.

Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com

3 comments:

Bhaumik said...

Amazing Artical Ritesh.. Its helps me a lot.

Ritesh Shah said...

Thank you Bhaumik for your warm response.

Pinal Dave said...

A quality Article.
I always talked about that you are great in .NET