Community Pick: Many members of our community have endorsed this article.

HTTP Handler to read webpage

Published:
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
4,705 Views

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.