Retrieving Traffic Information

To help motorists select the best route to a destination, the California Department of Transportation provides a website at which users can look up current conditions for specific highways. The CATraffic web service offered by XMethods lets you integrate the traffic report into your own programs and web pages.

The ASP.NET page CalTraffic.aspx at this book’s companion website lets you select a specific California highway. After you choose a highway and click the Get Traffic Conditions button, the page will display the highway’s current traffic conditions, as shown in Figure 1.13.

click to expand
Figure 1.13: Using the CATraffic web service to retrieve traffic information

Behind the Scenes of the CATraffic Web Service

The CATraffic web service calls the getTraffic method that programs can use to look up traffic conditions on a specific highway:

string getTraffic(string highway)

The source code in Listing 1.6 implements the ASP.NET page CalTraffic.aspx.

Listing 1.6 CalTraffic.aspx

start example
Public Class WebForm1     Inherits System.Web.UI.Page     Protected WithEvents Label1 As System.Web.UI.WebControls.Label     Protected WithEvents Label2 As System.Web.UI.WebControls.Label     Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox     Protected WithEvents Button1 As System.Web.UI.WebControls.Button     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox #Region " Web Form Designer Generated Code "   ' Code not shown #End Region Private Sub Button1_Click(ByVal sender As System.Object, _ Ä   ByVal e As System.EventArgs) Handles Button1.Click   Dim Highway As New net.xmethods.www.CATrafficService()   Dim WebError As Boolean = False   Dim Conditions As String   Try     Conditions = Highway.getTraffic(ListBox1.SelectedItem.ToString())   Catch Ex As Exception     WebError = True   End Try   If (WebError) Then     TextBox1.Text = "Error accessing Web service"   Else     TextBox1.Text = Conditions   End If End Sub End Class
end example

Within the button-click handler, the code first creates an object that the program will use to interact with the web service:

Dim Highway As New net.xmethods.www.CATrafficService()

Then, within a Try-Catch block, the code will call the getTraffic method. If the method is successful, the code will assign the traffic conditions to the text box. To create the CalTraffic.aspx ASP.NET page, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the name CalTraffic. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the label, buttons, and text boxes previously shown in Figure 1.13 onto the page. Using the Properties window, set the text box to support multiline operations. Also, add entries similar to those shown within the list box.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type http://www.xmethods.net/sd/2001/CATrafficService.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Enter the program statements previously shown.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net