30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1076 ASP .<strong>NET</strong> and Web Services Chapter 21<br />

Method UpdateWeatherConditions, which gathers weather data from a Web<br />

page, is the first WebMethod that a client must call from the Web service. The service also<br />

provides the WebMethods Cities, Descriptions and Temperatures, which<br />

return different kinds of forecast-related information.<br />

When UpdateWeatherConditions (line 32–93) is invoked, the method connects<br />

<strong>to</strong> a Web site containing the traveler’s forecasts from the National Weather Service (NWS).<br />

Line 37 creates a WebClient object, which we use because the WebClient class is<br />

designed for interaction with a source specified by a URL. In this case, the URL for the<br />

NWS page is http://iwin.nws.noaa.gov/iwin/us/traveler.html. Lines<br />

43–54 call WebClient method OpenRead; the method retrieves a Stream from the<br />

URL containing the weather information and then uses this Stream <strong>to</strong> create a Stream-<br />

Reader object. Using a StreamReader object, the program can read the Web page’s<br />

HTML markup line-by-line.<br />

The section of the Web page in which we are interested starts with the String<br />

“TAV12.” Therefore, lines 50–53 read the HTML markup one line at a time until this<br />

String is encountered. Once the string “TAV12” is reached, the Do/Loop While structure<br />

(lines 64–67) continues <strong>to</strong> read the page one line at a time until it finds the header line<br />

(i.e., the line at the beginning of the forecast table). This line starts with either day-<br />

Format, indicating day format, or nightFormat, indicating night format. Because the<br />

line could be in either format, the structure checks for both. Line 70 reads the next line from<br />

the page, which is the first line containing temperature information.<br />

The While structure (lines 72–85) creates a new CityWeather object <strong>to</strong> represent<br />

the current city. It parses the String containing the current weather data, separating the<br />

city name, the weather condition and the temperature. The CityWeather object is added<br />

<strong>to</strong> cityList (an ArrayList that contains a list of the cities, their descriptions and their<br />

current temperatures); then, the next line from the page is read and is s<strong>to</strong>red in inputLine<br />

for the next iteration. This process continues until the length of the String read from the<br />

Web page is less than or equal <strong>to</strong> 28. This signals the end of the temperature section. Line<br />

91 adds the ArrayList cityList <strong>to</strong> the Session object so that the values are maintained<br />

between method calls.<br />

Method Cities (line 95–113) creates an array of Strings that can contain as many<br />

Strings as there are elements in cityList. Lines 99–100 obtain the list of cities from<br />

the Session object. Lines 106–110 iterate through each CityWeather object in cityList<br />

and insert the city name in<strong>to</strong> the array that is returned in line 109. Methods<br />

Descriptions (lines 115–133) and Temperatures (lines 135–154) behave similarly,<br />

except that they return weather descriptions and temperatures, respectively.<br />

Figure 21.21 contains the code listing for the CityWeather class. The construc<strong>to</strong>r<br />

takes three arguments: The city’s name, the weather description and the current temperature.<br />

The class provides the properties CityName, Temperature and Description,<br />

so that these values can be retrieved by the Web service.<br />

1 ' Fig. 21.21: CityWeather.vb<br />

2 ' Class representing the weather information for one city.<br />

3<br />

4 Imports System<br />

Fig. 21.21 Class that s<strong>to</strong>res weather information about a city (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!