<

HTTP Handler to read webpage

Published on
10,556 Points
4,156 Views
4 Endorsements
Last Modified:
Approved
Community Pick
Generally in ASP.NET we are used to working 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 thought that you could bypass code-behind file?

ASXH (HTTP Handler) is really very powerful but under utilized utility. Even if you search GOOGLE, you won’t find many examples. 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 for 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;
        }
    }
}

Open in new window


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

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

Open in new window


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.

Hope you have enjoyed this read. do give me your feedback.

Thanks,

Ritesh Shah
4
Comment
Author:RiteshShah
0 Comments

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
This is Part-2 of Learning to use the Power of Mailwasher Pro so if you haven't watched Part-1 yet, I urge you to do so before watching this video. Click this link to watch Part-1 (https://www.experts-exchange.com/videos/56638/Learn-to-use-the-POWER…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month