Creating the Web Services Client

By the time you read this book, the code shown in Example 16-2 might not work. If Amazon has changed the layout of the book page, the parsing code could easily break. Screen-scraping is notoriously fragile. Fortunately, however, Amazon has provided a web service with methods that allow you to acquire the information you need based on ISBN or other criteria.

In the first revision of the desktop application, you’ll strip out all the screen scraping code and replace it with a web services client. To make this work, however, you’ll need to download the Amazon Web Services developer kit, available at http://www.amazon.com/webservices, and as shown in Figure 16-5.

Amazon.com Web Services

Figure 16-5.  Amazon.com Web Services

The key is to create a proxy class to use with your client. You do so by obtaining the WSDL document that Amazon supplies, and then compiling that with the command-line instruction:

wsdl /o:Amazon.cs AmazonWebServices.wsdl

Your next step is to add the resulting file (Amazon.cs) to your new project, which will, among other things, declare the ProductInfo class (an instance of which you’ll create in your own class). To get started, create a duplicate of the project shown in Example 16-2, and call it SalesRankDBWebServices, as shown in Example 16-5.

Example 16-5. SalesRankDBWebServices

using System; using System.Drawing; using System.Collections; using System.ComponentModel; ...

Get Programming C#, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.