Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Using the Amazon Web Service

  This article demonstrates consuming a Web service. It uses Amazon Web services to build an application that searches for books, music or movies based on keywords.

Difficulty: Easy
Time Required: 1-3 hours
Cost: Free
Software: Visual Studio Express Editions, Amazon SDK
Hardware:
Download: Lost!
For my inaugural post, I wanted to look at how client applications consume a Web service. I chose the Amazon Web services to build an application that searches for books, music or movies based on keywords.

While you can use the Amazon Web services for free, you first need to register with Amazon. Once you've registered, Amazon will email you a subscription identification code to include in the requests you send to the Web service. I'll show you how that works once we get into the code for the application.

Amazon recently released version 4.0 of its E-Commerce Web Services (ECS). Unfortunately, I discovered some problems using the ECS version, as documented here. So for this application, I used the previous 3.0 release of Amazon Web services.

The 3.0 release includes a downloadable software development kit. Before getting started on the application, I installed the SDK on my computer. The SDK provides all the information you need on using Amazon Web services to add content to Web sites, enable transactions, and upload products to Amazon.com. I don't need to add content to my Web site or use the more advanced features of the Amazon Web services. Not yet, anyway. I usually have modest objectives when I begin working with unfamiliar technology. A desktop application that uses one of the Web methods provided by the Amazon Web service is a good starting point. Once I get familiar with the Amazon Web services I can look forward to tackling more ambitious projects later on.

For this example, we'll use one of the new Visual Studio 2005 Express Editions. The Express Editions of Visual Studio 2005 are simplified, streamlined and lightweight versions of Visual Studio tailored for hobbyist, novice, and student developers. Beta 2 of the Express editions can be downloaded at http://msdn.microsoft.com/express. Any of the Visual Studio 2005 Express Editions allow you to build applications which consume Web services; I'm using Visual C# 2005 Express Edition in this example.

Prior versions of Visual Studio .NET made it very easy to incorporate a Web service in an application. Visual Studio 2005 is no different. The easiest and most direct method is to use the Add Web Reference dialog, accessible from either the Project menu or by right-clicking on the project in Solution Explorer. The Add Web Reference dialog lets you browse your local machine or external locations for available Web services. For the Amazon Web service, I copied in the path to the Web Services Description Language (WSDL) file from the “DTD, XSD and SOAP WSDL” page in the References section of the API Guide. A WSDL (pronounced “wiz-dull”) is an XML-based representation of all the information you need to use a particular Web service, including the locations (or endpoints) for sending messages, descriptions of the operations provided by the Web service and how they need to be formatted, and the types of data you can include in a message.

I then clicked the “Go” button and the wizard went off to find the WSDL file. After a second or two the wizard displayed a service description for the Amazon Web service. The wizard added a default Web reference name of  “com.amazon.soap”. I changed that to something simpler, “Amazon”. When I clicked the Add Reference button, Visual C# Express automatically translated the WSDL file into a class (contained in the hidden project file Reference.cs). This class serves as a proxy for interacting with the Web service using managed types without having to bother with the gory details of SOAP or XML serialization or how to invoke a Web service. Visual C# Express also outfits the class with asynchronous versions of all the Web methods it discovers in the WSDL. All this behind-the-scenes work frees you to concentrate on the details of working with the methods and data exposed by the Web service to do interesting things in your application.

To simplify my code, I added a using statement at the top of the Form1.cs file for the namespace containing the Amazon Web service proxy class.

Visual C#

using WSFun.Amazon;

Visual Basic

Imports WSFun.Amazon

To perform a keyword search of the Amazon product listing, you need to call the KeywordSearchRequest Web method. This method requires a KeywordRequest input parameter, so my first task was to create an instance of a KeywordRequest object and set its various properties.

Visual C#

KeywordRequest keywordReq = new KeywordRequest();
keywordReq.locale = "us";
keywordReq.type = "lite";
keywordReq.sort = "reviewrank";
keywordReq.mode = this.SearchMode;
keywordReq.keyword = this.searchText.Text;
keywordReq.tag = this.SubscriberID;
keywordReq.devtag = this.SubscriberID;

Visual Basic

Dim keywordReq As KeywordRequest = New KeywordRequest()
keywordReq.locale = "us"
keywordReq.type = "lite"
keywordReq.sort = "reviewrank"
keywordReq.mode = Me.SearchMode
keywordReq.keyword = Me.searchText.Text
keywordReq.tag = Me.SubscriberID
keywordReq.devtag = Me.SubscriberID

I used hard values for the type and sort properties; I could have added user controls to the form to manipulate those values at runtime (for example, to sort the results alphabetically or by their sales rank rather than the hard-coded customer review rank). The mode and keyword properties are dynamic. The mode property controls which product line to search. Here, I use the value specified in the selected radio button on the form: either Books, Music or DVDs. I added a common handler for each of the radio button's CheckedChanged event to update the private member SearchMode to the appropriate string value used in the request.

Visual C#

keywordReq.tag = this.SubscriberID;
keywordReq.devtag = this.SubscriberID;

Visual Basic

keywordReq.tag = Me.SubscriberID
keywordReq.devtag = Me.SubscriberID

In the final lines of this block of code, I set the credentials for the request using the subscription identifier I obtained from Amazon. For version 3.0 of the Amazon Web services, you need to add this value to both the tag and devtag properties of the KeywordRequest. I accessed the subscription identifier using a private property. This makes the code more manageable. I stored the actual subscriber id in the application's configuration file; this allows me to change the id without having to recompile the application. If you download the code accompanying this article, be sure to update the app.config file to use the subscriber identification you obtain from Amazon.

Next, I created an instance of the Web service proxy class and called the KeywordSearchRequest method, passing in the KeywordRequest object as an input parameter.

Visual C#

AmazonSearchService amazonWS = new AmazonSearchService();
ProductInfo productInfo = amazonWS.KeywordSearchRequest(keywordReq);

Visual Basic

Dim amazonWS As AmazonSearchService = New AmazonSearchService()
Dim productInfo As ProductInfo = amazonWS.KeywordSearchRequest(keywordReq)

The KeywordSearchRequest returns a ProductInfo object, which includes a Details property. This property is actually an array of Details objects representing the items that matched my search criteria. After first checking to make sure the Details property contains items, I add the Details array to the Items collection of the Listbox control on the form.

Visual C#

this.searchResults.Items.AddRange(productInfo.Details);

Visual Basic

Me.searchResults.Items.AddRange(productInfo.Details)

In order to correctly display the name of the item returned in my search, I used the cool new partial class feature of Visual Studio 2005 to extend the Details class that Visual C# created when I imported the WSDL file. To do this, I first created a new class file for the project and named it Details_2.cs. I included the new partial keyword in the class declaration and then added an override for the ToString method. The ToString method is inherited by every class from the .NET Framework's base Object class and is used by the Listbox control for displaying a value for listed items.

Visual C#

public override string ToString()
{
return ProductName;
}

Visual Basic

Overrides Function ToString() As String
Return ProductName
End FunctionFunction

This override uses the ProductName property of the Details object. This lets me display the product information in the list box.

I also added a new property to the partial class, called ItemImage. I used this property to return the image associated with a selected product item.

Visual C#

WebRequest webReq = WebRequest.Create(this.ImageUrlMedium);
WebResponse webResp = webReq.GetResponse();
_image = Image.FromStream(webResp.GetResponseStream());

Visual Basic

Dim webReq As WebRequest = WebRequest.Create(Me.ImageUrlMedium)
Dim webResp As WebResponse = webReq.GetResponse()
_image = Image.FromStream(webResp.GetResponseStream())

The code in the property's get method uses a System.Net.WebRequest object to return the image specified in the ImageUrlMedium property of the Details object. In the handler for the ListBox's SelectedIndexChanged event, I access this property to update the image displayed on the form.

Visual C#

this.itemPicture.Image = tempDetails.ItemImage;

Visual Basic

Me.itemPicture.Image = tempDetails.ItemImage

This completes the tour of this application. Download Visual C# 2005 Express Edition and explore this application for yourself. See http://msdn.microsoft.com/express.

Follow the Discussion

  • H. KashaniH. Kashani

    Hi

    This is the error I get when I run it. Can you please help:

    Regards,

    Message 1 Could not find schema information for the element 'applicationSettings'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 12 3 Amazon Search

    Message 2 Could not find schema information for the element 'WSFun.Properties.Settings'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 13 4 Amazon Search

    Message 3 Could not find schema information for the element 'setting'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 14 5 Amazon Search

    Message 4 Could not find schema information for the attribute 'name'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 14 13 Amazon Search

    Message 5 Could not find schema information for the attribute 'serializeAs'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 14 28 Amazon Search

    Message 6 Could not find schema information for the element 'value'. C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\app.config 15 6 Amazon Search

    Error 7 'System.Windows.Forms.Application' does not contain a definition for 'EnableRTLMirroring' C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\Program.cs 20 25 Amazon Search

    Warning 8 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings' C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\Web References\Amazon\Reference.cs 50 33 Amazon Search

    Warning 9 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings' C:\Documents and Settings\HK\My Documents\My Course Materials\Internet Develpment II\AmazonSearch\AmazonSearch_VCS\Form1.cs 63 37 Amazon Search

  • Amol chaudhariAmol chaudhari

    hi,

    i am a web developer.i have used amazon web service 3.0.Everything is working fine except-:

    When i search for "C++"  in

    "Miscellaneous" mode, i get exception as "We can't ur request at our end.pl try later."

    If i want to search in "Miscellaneous" mode what should i send in "keywordReq.mode" for "Miscellaneous" mode.So that i get the result for C++.

    Please help me out.

    Thanks in advance.

    Thanks,

    Amol chaudhari

  • Pate LIVE!  » Blog Archive   » Creating an application using Amazon Web ServicePate LIVE! » Blog Archive » Creating an application using Amazon Web Service

    PingBack from http://blog.patelive.com/2007/06/11/creating-an-application-using-amazon-web-service/

  • C# - Project: Sklep » P??wnywarka cen - cos dzia??aC# - Project: Sklep » P??wnywarka cen - cos dzia??a

    PingBack from http://camelsite.ovh.org/devblog/?p=15

  • CarlosCarlos

    This is exaclty what i needed, thanks a lot!

  • LissethLisseth

    Thanks this article was very useful.

  • ken partridgeken partridge

    Does any one know were this code is now ? I cant find it on the new site

  • Clint RutkasClint I'm a "developer"

    @ken partridge:  Thanks for the heads up, working on getting that corrected

  • piyushpiyush

    h! all,

    i am piyush . can any one tell me how how do i list itemsusing web services of amazon.com. is it possible?

    please reply ASAP.

  • Clint RutkasClint I'm a "developer"

    @piyush:  check out the Amazon SDK.  They tell you how to do just that.

  • EvansEvans

    Very nice article - though its written in C#, I have been able to adapt it to work as a Java sample App.

    Nice one!

  • xuanthuyitxuanthuyit

    I did as suggestion but the application desn't run.

    An error occur ( the remote name cannot be solved)

    I also cannot dowload the posted example.

    the link http:\\soap.amazon.com doesn't run.

    Please help by sending me the example. kienvang1090@yahoo.com

    Thanks,

    Thuy

  • Clint RutkasClint I'm a "developer"

    @xuanthuyit check out http://blogs.msdn.com/coding4fun/archive/2006/12/16/1308209.aspx.

  • tomnytomny

    Looks good, exactly what I need.

    The download link is broken, is it possible to get the project somewhere else ?

    Thanks,

    Tom

  • Clint RutkasClint I'm a "developer"

    @tomny, GotDotNet sites are semi lost.

    Try:

    http://blogs.msdn.com/coding4fun/archive/2006/12/16/1308209.aspx

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.