Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

CProgramTrends
FlashNewz
DevWebPro








C# - Screen Scraping

By Mads Kristensen
Expert Author
Article Date: 2007-02-14

Some say that screen scraping is a lost art because it is no longer an advanced discipline.

That may be right, but there are different ways of doing it. Here are some different ways that all are perfectly acceptable, but can be used for various different purposes.

Old school

It's old school because this approach has existed since .NET 1.0. It is highly flexible and lets you make the request asynchronously.

public static string ScreenScrape(string url)
{
  System.Net.WebRequest request = System.Net.WebRequest.Create(url);
  // set properties of the request
  using (System.Net.WebResponse response = request.GetResponse())
  {
   using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
   {
   return reader.ReadToEnd();
   }
  }
}


Modern

In .NET 2.0 we can use the WebClient class, which is a cleaner way of solving the same problem. It is equally as flexible and can also work asynchronous.

public static string ScreenScrape(string url)
{
  using (System.Net.WebClient client = new System.Net.WebClient())
  {
  // set properties of the client
   return client.DownloadString(url);
  }
}


The one-liner

This is a short version of the Modern approach, but it deserves to be on the list because it is a one-liner. Tell a nineteen ninetees developer that you can do screen scraping in one line of code and he wont believe you. The approach is not flexible in any way and cannot be used asynchronously.

public static string ScreenScrape(string url)
{
  return new System.Net.WebClient().DownloadString(url);
}


That concludes the medley of screen scraping approaches. Pick the one you find best for the given situation.

Comments

Tag:

Reddit | Furl

About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

C Programming Trends is an iEntry, Inc. ® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal