<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

HTTP Handler to read webpage

Published on
10,616 Points
4,216 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

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Join & Write a Comment

Learn the basics of Skype For Business in office 365
Watch this simple and effective video tutorial to extract attachments from Outlook 2007 and try this easy method by yourself. No need to go anywhere, just watch the video and export attachments from Outlook in few simple steps. To know more, click h…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month