- 作者:zhaozj
- 发表时间:2020-12-23 10:35
- 来源:未知
In this article we will be utilizing the Amazon Web Service (AWS) from an ASP.NET web form. Instead of taking a comprehensive look at the entire API we will concentrate on a single method to retrieve product information. In developing the sample we will take a look at abstraction techniques to make the web service easier to use in a web form scenario.
Our goal in this article is to build a user control which we can place on a web form. The user control will retrieve information about a book and display the information, such as price and availability, with a minimum of effort from the form developer. The ideal usage would look similar to the following:
<%@ Register TagPrefix="uc1" TagName="Amazon_ShowBook" Src="Amazon_ShowBook.ascx" %>...<uc1:Amazon_ShowBook id="Amazon_ShowBook1" runat="server" ASIN="1904811000"/>
ASIN is short for “Amazon Standard Item Number”. For books, the ASIN is the same as the ISBN. Every product on amazon.com has as ASIN which you can find on the product’s page. The end result should look like the following screen shot:
Setting Up References
For starters, you’ll need to download the Amazon.com Web Services Developer’s Kit. The kit is free, although you must apply for your own, unique developer token to use in the web service calls, as we will see later. It is important for you to read the terms and conditions before beginning development, as amazon.com has specific rules on how you use the API and display the information, including how frequently you can make calls to the API.
Once the kit and developer token are in hand, you are ready to begin a new web project. Right click the project and select “Add Web Reference”. In the dialog box that follows, we will go to the URL for the WSDL describing AWS at http://soap.amazon.com/schemas3/AmazonWebServices.wsdl. Before clicking the Add Reference button you might want to give the web reference a different name (in the lower right textbox), in this example we will use AmazonWS. After clicking the Add Reference button you’ll have a proxy class in your project for the web service. Calling methods on the proxy class will result in a network hop across the Internet to amazon’s servers, where the information you request will be located and sent back to your application. Let’s take a look at a simple example.
Search Request, First Attempt
The following code snippet demonstrated a quick and dirty call to Amazon. We will discuss some of the basics about this call before moving on to improve the code.
AmazonWS.AsinRequest asinRequest = new AmazonWS.AsinRequest(); asinRequest.asin = "1904811000"; asinRequest.devtag = "1234567890AAA"; asinRequest.type = "heavy"; asinRequest.tag = "webservices-20"; AmazonWS.AmazonSearchService amazonSearch = new AmazonWS.AmazonSearchService(); AmazonWS.ProductInfo awsInfo; awsInfo = amazonSearch.AsinSearchRequest(asinRequest); priceLabel.Text = awsInfo.Details[0].OurPrice;