Connecting to Amazon Web Service through ASP.NET

I have had such fun this afternoon! I’ve been creating a little sample application for something I’m doing and I was sick of the Northwind database so I thought I’d try hooking up to the Amazon web service instead (and make fun of David Hasselhof’s 80s movies).

I went the VS2005 Web References route and it was actually pretty painless once I figured out how to use all the different objects I needed.

Adding the web service to my project and creating all the objects needed to query it was as easy as pasting the wsdl link into the Add Web Reference dialog in VS2005. I thought that was quite impressive considering what’s happening behind the scenes.

Writing the code took a bit of trial and error while I figured out which objects I’d need to call to talk to the web service, but between the Amazon documentation and a couple of good .NET tutorials I managed to figure it out.

Here’s the code I ended up with to request all the DVDs for a particular actor:

public static DataSet GetDvdByActor(string actor){
	// create an amazon webservice object
	AWSECommerceService amazon = new AWSECommerceService();

	// create a search request
	ItemSearchRequest request = new ItemSearchRequest();
	request.SearchIndex = "DVD";
	request.Actor = actor;
	request.Sort = "salesrank";
	request.ResponseGroup = new string[] { "Medium" };
	request.Count = "20";

	// send the search request to amazon
	ItemSearch search = new ItemSearch();
	search.AWSAccessKeyId = ConfigurationManager.AppSettings["AWSAccessKeyId"];
	search.Request = new ItemSearchRequest[] { request };

	// process the results
	ItemSearchResponse response = amazon.ItemSearch(search);
	DataSet dataset = CreateDvdDataSet(response);
	return dataset;

This is the code I used to actually process the results and put them into a data object for the rest of my application to use (yes I used datasets.. it’s a sample application!):

private static DataSet CreateDvdDataSet(ItemSearchResponse response)
	// create dataset and data table
	DataSet dataset = new DataSet();
	DataTable table = new DataTable("dvds");

	foreach (Item item in response.Items[0].Item)
		// create rows from information in results
		DataRow row = table.NewRow();
		row["Title"] = item.ItemAttributes.Title;
		row["DetailsPageUrl"] = item.DetailPageURL;
		if (item.SmallImage != null)
			row["ImageUrl"] = item.SmallImage.URL;
		row["Rating"] = item.ItemAttributes.AudienceRating;
		row["Release"] = item.ItemAttributes.ReleaseDate;
		row["TheatricalRelease"] = item.ItemAttributes.TheatricalReleaseDate;
	// return dataset	dataset.Tables.Add(table);
	return dataset;

The best documentation I found was actually in the web service itself. When I couldn’t figure out the exact parameters to put in I tried putting the equivalent REST url into the browser and manually setting parameters.

The good thing is if you set a parameter to the wrong thing you get a really useful error message that tells you what should have put:
Your ResponseGroup parameter is invalid. Valid response groups for requests include Request, ItemIds, Small, Medium, Large, Offers, OfferFull, .. ListmaniaLists, SearchInside, PromotionalTag, SearchBins.

Main amazon web service site – you can sign up for an account here and access all the code samples and documentation
Amazon documentation – useful, but not .NET specific
Site Point – Using WebServices in ASP.NET – only walkthrough I could find for Amazon Web Services 4.0
Coding4Fun – Connecting to Amazon web service through VS2005 – older amazon web service version, but still has some good VS2005 information

Posted on 16 Mar 06 by Helen Emerson (last updated on 16 Mar 06).
Filed under ASP.NET