Library tutorials & articles

Retrieving HTTP content in .NET

Introduction

HTTP content retrieval is an important component for applications these days. Although .NET reduces the need to explicitly retrieve content from the Web through built-in mechanisms in the Web Services framework, ADO.NET and the XML classes, there are still many needs to retrieve Web content directly and manipulate it as text or data downloaded into files. In this article Rick describes the functionality of the HttpWebRequest and HttpWebResponse classes and provides an easy to use wrapper class. The class simplifies the HTTP access and provides most of the common features in an easy to use single interface while still providing full access to the base functionality of the HttpWebRequest class. In the process Rick describes some sticky issues like string encoding and Cookie handling and some related topics like implementing events and running multiple threads to service Web requests.

Last week I decided I needed a good, useful project to throw at .NET to continue my learning curve while actually building something I have a use for. A few years back I'd written a Web Monitoring package that allows me to monitor a set of Web sites and send out alerts when the sites are down and not responding. The app has been showing its age and since it was developed using C++ it has a clunky user interface that's not very maintainable. I thought this would be a good 'training' application to re-build for .NET. This application exercises HTTP functionality built into the .NET Framework, requires setting up and running multiple threads, hooking up events and managing a small set of data without a database backend and finally providing a Windows Form UI. In the future converting this application to work as a Windows service would also be a nice feature. This application is one that lets me explore a wide variety of features of a programming environment. In this month's article I'll describe a few of the features that I needed to build focusing specifically on the HTTP retrieval mechanism.

Comments

  1. 10 Jul 2007 at 15:29
      if (oReqCookie.Name ==
            oRespCookie.Name)  {
            oReqCookie.Value =
                   oRespCookie.Name;

    Do you mean?








     if (oReqCookie.Name ==
            oRespCookie.Name)  {
            oReqCookie.Value =
                   oRespCookie.Value;




  2. 12 Mar 2007 at 18:57

    You have a paragraph that states:

    "The most common use of delegates is an eventhandler, which uses the delegate to fire events. When the event publisher fires the event method, the delegate that is assigned to handle the event is called and you're event subscriber object then can simply handle the event by implementing a method in your class."

    Unfortunately, for someone trying desparately to learn C#, this sounds like:

    "blah blah blah blah blah blah"

    I'm sorry but could you publish a REAL SIMPLE example and explanation of asynch calls in Asp.net/C#.  I have been all over the web looking for one and they all assume you already know C# so all the delegate calling callback calling method calling delegate calling calling callingcallingcalling.................has just finally gotten on my nerves.

  3. 29 Dec 2006 at 22:27
    I'm just new to c# coming from VB.NET.  This is a great example!  Thanks
  4. 10 May 2006 at 04:30

    hi

    i have similar problem like you (in a previous time) with in using httwebrequest to login in a website... especially how to handle event clicking ex: onkeypress="checklogin()"

    i hope you can help me to solve the problem..

     

    thnx b4

  5. 18 Oct 2005 at 02:51

    hi
    I use httpwebrequest to login into a website (using Networkcrendential). When i got that page i need to click a link. How can we achieve this (axwebbrowser ?) and it contains frames. And also after clicking that menu in second page i have to click a Javascript button. Is there anyway to achieve this.


    Rajesh

  6. 25 Mar 2005 at 00:48
    Your article is very good and your source code is a good start.  It has some cool features but is lacking some good programming practices.  Most noteable is the lack of finally blocks used.  You should try to insure that the Close() method on the WebRequest and WebResponse objects are always closed before the object reference goes out of scope.  You can ensure that happens by placing your code in a try/finally block.

    Also, you should consider having your class implement the IDisposable interface.  And in that implementation clean up, close, and release the resources used by your private variables.
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Retrieving HTTP content in .NET.

Leave a comment

Sign in or Join us (it's free).

Rick Strahl
AddThis

Related discussion

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

We'd love to hear what you think! Submit ideas or give us feedback