Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Weather Update application

Rate me:
Please Sign up or sign in to vote.
3.86/5 (7 votes)
26 Apr 20031 min read 53.9K   974   33   1
This article is intended to show you how you can use the Internet to gather and display dynamic content.

Sample Image - WeatherUpdate.gif

Introduction

This article is intended to show you how you can use the Internet to gather and display dynamic content.

Background

While teaching myself C#, I wanted to figure out how to use the HttpWebRequest and HttpWebResponse objects to gather information from the Internet. Being new to C#, I decided to rewrite one of my previous C++ applications in C#. What better way to learn the caveats of a new language than to rebuild an existing application in a newer language?

I’d like to thank Mike Gold from C# corner for his articles, Carlos Perez for his progress bar demo, Dion Heskett for his gradient label and Nishant S for his notify icon example. I’m an avid Code Project reader and appreciate all the hard work others have done.

The Code

C#
string sfullpath = 
   @"http://asp.usatoday.com/weather/" + 
   @"cityforecast.aspx?LocationID=USVA0023&ps=L1";
                                             
HttpWebRequest MyWebReq;
HttpWebResponse MyWebRes;
StreamReader MySReader;
 

sForecast = "";
sTemp = "";
string sTempPrompt = @"font-size:20px"; 

// Get request & response from the internet.
MyWebReq = (HttpWebRequest) WebRequest.Create(sfullpath);
MyWebRes = (HttpWebResponse) MyWebReq.GetResponse();     
MySReader = new 
  StreamReader(MyWebRes.GetResponseStream(), Encoding.ASCII);

// Get the line of code the prompt is on.

while( sTemp.IndexOf( sTempPrompt ) == -1 )
{
    sTemp = MySReader.ReadLine();
}

MySReader .Close();
sTemp = sTemp.ToLower();
sTemp = sTemp.Trim();

sForecastOriginal = sTemp;

// Locate the index of the prompt.

int iTempIndex = sTemp.IndexOf( sTempPrompt );
iTempIndex = iTempIndex + 16;

sForecastOriginal = sForecastOriginal.Substring( iTempIndex, 600 );
sForecast = sForecastOriginal;

sTemp = sTemp.Substring( iTempIndex, 5 );
sTemp = sTemp.Substring( 0, sTemp.IndexOf( "<" ) );

// Trim string for displaying.
sTemp = sTemp.Trim();

//...

Using the code

The one main point I’d like to make is that since I’m parsing HTML, I’m at the mercy of the website where I’m getting the HTML from. If they decide to dramatically change the setup of their page, then this application might not get the correct data. If a message box pops up, shut the program down and then open it back up again. When the message box shows up, that means the HTML I'm trying to parse is being written.

History

Initial release 1.0.1

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Doug graduated college in 2000 with a degree in
Computer Information Systems. Since then Doug
has been working on software engineering projects
mostly for government consulting companies.
The majority of Doug's programming experience is in
windows development using C# and visual C++ with MFC.
Since October 2002, Doug has been using C# and has
been creating C# windows applications and ASP.NET web applications.

Comments and Discussions

 
GeneralInteresting Pin
dog_spawn27-Apr-03 12:26
dog_spawn27-Apr-03 12:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.