web service of Amazon

转自:http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=53
Consuming the Amazon.com Web Services Print Printable Version
Author: James Foster Date Posted: 07/17/2002 .NET Version: 1.0.3705
I received an interesting email from Amazon. Normally, the emails I received from them are either the promotional for bestsellers that I am not interested in, or the latest ranting from Bezos. However, this email was not a solitication for books, but rather a notification for those in the Amazon.com Associates program. A section of the email read:

Build your own Amazon.com Product Detail Page  
...with Amazon.com Web Services! Powered by XML, this new platform enables you to display dynamic product information and our best merchandising features (Customer Reviews, Customers Also Bought, Add to Shopping Cart, and more)."

I've always been interested in getting price quotes on books programmatically, but did not want to go through the process of screen scraping the Amazon.com website, and keeping track of any changes to the html rendering. So, as any eager .netCoder, I went to Amazon's site to see just what they had to offer. Here are the steps I went through in utilizing Amazon's web services.

Downloading the SDK
The first step is downloading the SDK. This contains documentation, examples in Java and Perl, schemas for the web service responses, and sample XSL transformations. Also included is the WSDL for the web service. The SDK can be downloaded here:

Signing Up
Once armed with the SDK, the next step is registering with Amazon and getting a developer key, which is termed a Token. If you have an existing Amazon.com account, you can use that. The process only asks for your email and a password, and that you agree with the terms and conditions of the service. You can apply for a Developer's Token here:

Generating the Proxy Class
For web services written using .NET, you typically have a URL of an .asmx file. By appending ?wsdl to this URL, you can get the WSDL for the web service. In the case of the Amazon.com web service, the WSDL is in the downloadable SDK in the /WSDL directory, and is called AmazonWebServices.wsdl. To generate a proxy class for this service, open the Command Prompt (preferably the Visual Studio.NET Command Prompt, which verifies the PATH environmental parameters are set correctly). Navigate to the /WSDL directory containing the AmazonWebServices.wsdl file. Type the following at the command prompt:
wsdl /o:AmazonWebServices.cs AmazonWebServices.wsdl
This uses the default parameter values to create a SOAP proxy class in C#, and as we indicated, is saved as AmazonWebServices.cs.

Using the Proxy Class
Once you have the Proxy Class, you can add it to your project. If you do not have an existing Visual Studio.NET project already, create one now. Then, drag the .cs file into your project. I created a C# WinForms application, and added the class to the project. I then set a reference to the System.Web and System.Web.Services assemblies (Project->Add Reference), since they are used by the Proxy Class, and are not included by default in WinForms applications.

I decided to test the webservice by looking up the details of the book, The Analects of Confucius. To display the results, I I created a single textbox on the form, and accepted the default name of textBox1. In the Form's Load method, I wrote the following code to invoke the webservice.

//Create Instance of Proxy Class
AmazonSearchService as1 = new AmazonSearchService();

//Create Encapsulated Request
AsinRequest asin = new AsinRequest();
asin.devtag = "USE_YOUR_TOKEN_HERE";
asin.asin = "0679722963";
asin.type = "lite";
asin.tag = "webservices-20";
asin.version="1.0";

//Call Service and Get Product Info
ProductInfo pi = as1.AsinSearchRequest(asin);

//Display Product Details
textBox1.Text += pi.Details[0].ProductName + " costs " + pi.Details[0].OurPrice;
C# VB

I then ran the form, and received the current price of the book I was interested in.

TIP: I did encounter one error in developing this service that you should watch out for. The service expects the request parameters to be given values. I expected default values, and only set the Asin I was interested in. The result was a 404 error. If you get this error, check that you are initializing all the request parameters.

About the Service
Calls to the web service are encapsulated in various request objects, such as AsinRequest (Asin is an Amazon identifier, equal to the ISBN for books), UpcRequest, AuthorRequest, KeywordRequest, and many more. This allows you to programmatically access the majority of cataloging features of the Amazon site.

These calls return a ProductInfo object, which contains an array of Details objects. For a request for a specific ISBN/ASIN, there will be only one element in this array if a match was found, but several of the other requests can return multiple matches. The Details object contains over 40 properties about the product, including the product name, the authors, the list price, Amazon's price, the availability, and the image URL in various sizes. Consult the SDK documentation for complete details about the available properties.

With the release of these web services (labeled as beta), Amazon is taking a proactive step in reaching out to developers. There is more to the services than has been covered here. To learn more, visit Amazon's Web Services homepage:


posted @ 2005-08-26 21:53  荒芜  阅读(848)  评论(0编辑  收藏  举报