Retrieving Book Information

On the Web, Amazon (amazon.com) and Barnes & Noble (barnesandnoble.com) are two of the largest online booksellers. Both sites let users shop for books electronically. To integrate the capabilities of these two online sites into your own programs and web pages, you can take advantage of web services.

To start, Amazon offers a software development kit (SDK) programmers can use to search for books, videos, and music, and by keyword, author, artist, and more. Further, programmers can integrate support for the Amazon shopping cart into their own applications and websites.

You can download the Amazon web services SDK from the Amazon website at www. amazon.com/webservices. After you download the software development kit, you must apply for a developer’s token (a key) that you must include as a parameter within your function calls to the services.

Second, the BNQuote web service returns the price of a book at Barnes & Noble for a given ISBN.

The ASP.NET page AmazonDemo.aspx on this book’s companion website uses the Amazon web services to list the titles and prices of various Sybex books at Amazon. When you display the page and click on the Get Amazon.com Pricing button, the page will use the Amazon web service to perform a keyword search on “Sybex.” The page will place the search results for the first 50 books within the text box, as shown in Figure 1.10.

Likewise, the ASP.NET page BarnesAndNoble.aspx at this book’s companion website displays buttons for several different book titles. If you click one of the buttons, the page will display the book’s current price at the online Barnes & Noble store, as shown in Figure 1.11.

click to expand
Figure 1.10: Using the Amazon web services SDK within an ASP.NET page

click to expand
Figure 1.11: Using the BNQuote Web service to display book prices at Barnes & Noble online

Behind the Scenes of the Amazon Web Service

The Amazon web SDK provides several different web services. To search Amazon products, you use the AmazonSearchService, passing to the service a parameter that specifies whether you want to perform a keyword search or search by author, artist, and so on. To interact with the Amazon shopping cart, you would use a different web service. Listing 1.3 implements the ASP.NET page AmazonDemo.aspx.

Listing 1.3 AmazonDemo.aspx

start example
Public Class WebForm1     Inherits System.Web.UI.Page     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox     Protected WithEvents Label1 As System.Web.UI.WebControls.Label     Protected WithEvents Button1 As System.Web.UI.WebControls.Button #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 AmazonQuery As New com.amazon.soap.AmazonSearchService()   Dim KeywordRequest As New com.amazon.soap.KeywordRequest()   Dim WebError As Boolean = False   Dim ProductInfo As com.amazon.soap.ProductInfo   KeywordRequest.devtag = "XXXXXXXXXXXXXX"  'Replace X's with your key.   KeywordRequest.keyword = "Sybex"   KeywordRequest.mode = "books"   KeywordRequest.type = "heavy"   KeywordRequest.tag = "webservices-20"   KeywordRequest.version = "1.0"   Dim Page As Integer   For Page = 0 To 4     KeywordRequest.page = Page.ToString()     Try       ProductInfo = AmazonQuery.KeywordSearchRequest(KeywordRequest)     Catch Ex As Exception       WebError = True       TextBox1.Text = "Error in Web service " & Ex.Message     End Try     If (Not WebError) Then       Dim Info As com.amazon.soap.Details       For Each Info In ProductInfo.Details        TextBox1.Text &= Info.ProductName & " " & Info.OurPrice & vbCrLf       Next     End If    Next End Sub End Class
end example

As discussed, before a program can use a web service, the program must create an object that corresponds to the web service. The following statement creates a variable named AmazonQuery that corresponds to the AmazonSearchService:

Dim AmazonQuery As New com.amazon.soap.AmazonSearchService()

To perform a keyword search, the code must create a variable that specifies the search criteria. The following statement creates a variable to hold the search fields:

Dim KeywordRequest As New com.amazon.soap.KeywordRequest()

Then, the following statements specify the search criteria. The Amazon web services software development kit briefly describes the various fields. You must change the statement that assigns X’s to the devtag field to contain the developer tag you download from the Amazon web service site:

KeywordRequest.devtag = "XXXXXXXXXXXXXX"  'Replace X's with your key. KeywordRequest.keyword = "Sybex" KeywordRequest.mode = "books" KeywordRequest.type = "heavy" KeywordRequest.tag = "webservices-20" KeywordRequest.version = "1.0"

By default, the Amazon web service search operation will return 10 matching products. To display 50 Sybex books, the code repeatedly performs the search operation within a For Each loop. Note that the code calls the service’s KeywordSearchRequest method within a Try-Catch block to detect any exceptions the service may generate.

To build the 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 AmazonDemo. 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 box previously shown in Figure 1.10 onto the page. Using the Properties window, set the text box to support multiline operations.

  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 the URL http://soap.amazon.com/schemas/ AmazonWebServices.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.

Behind the Scenes of the Barnes & Noble Web Service

The BNQuote web service supports one method, getPrice, which returns the price for a book based on an ISBN number:

single getPrice(string ISBN)

Listing 1.4 implements the ASP.NET page BarnesAndNoble.aspx.

Listing 1.4 BarnesAndNoble.aspx

start example
Public Class WebForm1     Inherits System.Web.UI.Page     Protected WithEvents Label1 As System.Web.UI.WebControls.Label     Protected WithEvents Button1 As System.Web.UI.WebControls.Button     Protected WithEvents Button2 As System.Web.UI.WebControls.Button     Protected WithEvents Button3 As System.Web.UI.WebControls.Button     Protected WithEvents Button4 As System.Web.UI.WebControls.Button     Protected WithEvents Label2 As System.Web.UI.WebControls.Label #Region " Web Form Designer Generated Code "     ' Code not shown. #End Region     Private Sub Page_Load(ByVal sender As System.Object, ByVal e_ ÄAs System.EventArgs) Handles MyBase.Load         'Put user code to initialize the page here     End Sub     Private Sub ShowPrice(ByVal Title As String, ByVal ISBN As String)         Dim BNQuery As New net.xmethods.www.BNQuoteService()         Dim Price As Single         Dim WebError As Boolean = False         Try             Price = BNQuery.getPrice(ISBN)         Catch Ex As Exception             WebError = True             Label2.Text = "Web service error: " & Ex.Message         End Try         If (Not WebError) Then             If (Price = -1) Then                 Label2.Text = Title & " not found"             Else                 Label2.Text = Title & " Price: " & Price.ToString()             End If         End If     End Sub     Private Sub Button1_Click(ByVal sender As System.Object, _ Ä      ByVal e As System.EventArgs) Handles Button1.Click         ShowPrice(Button1.Text, "0672319225")     End Sub     Private Sub Button2_Click(ByVal sender As System.Object, _ Ä      ByVal e As System.EventArgs) Handles Button2.Click         ShowPrice(Button2.Text, "0072223189")     End Sub     Private Sub Button3_Click(ByVal sender As System.Object, _ Ä      ByVal e As System.EventArgs) Handles Button3.Click         ShowPrice(Button3.Text, "0596002246")     End Sub     Private Sub Button4_Click(ByVal sender As System.Object, _ Ä      ByVal e As System.EventArgs) Handles Button4.Click         ShowPrice(Button4.Text, "0596000952")     End Sub End Class
end example

As you can see, the program defines handlers that correspond to each of the form’s buttons. Within each handler, the code calls the ShowPrice function, passing to the function the name of the corresponding book and the book’s ISBN. Within the ShowPrice function, to interact with the BNQuote web service, the code first creates an object that corresponds to the service:

   Dim BNQuery As New net.xmethods.www.BNQuoteService()

Then, within a Try-Catch block, the code calls the service’s getPrice method. If the service cannot find the book based on the specified ISBN, the getPrice method will return the value -1.

To build the BarnesAndNoble.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 BarnesAndNobleDemo. 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 box previously shown in Figure 1.11 onto the page.

  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 the URL http://www.xmethods.net/sd/2001/ BNQuoteService.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