Web services are some of the most exciting emerging technologies. Web services in general means services offered via the web. They are the new service-oriented approach to programming. Web services are based on building applications by discovering and implementing available services or invoking available applications to complete a task.
In this article we are going to learn to implement keyword searching, the default searching capability when you go to amazon.com. Moreover we will utilize the SOAP option that allows Amazon web services to be accessed through SOAP requests and responses providing a more object-oriented approach to integrate data into your application. The documentation for the Amazon Web service is available at Amazon in the developer's kit.
For the purpose of this article you will need the .NET framework installed, you will need a free token (license key) from Amazon, which you can get here. (Make sure to read the Terms of Service before signing up for the token.) Moreover you will need Visual Studio .NET if you want to experiment with source code presented in this article.
Open your Visual Studio .NET, create a new ASP.NET Web Application Project, which we will call amazonproject, click OK as shown in figure 1.
Figure 1: Creating a web application
Adding Web Reference
Now we will add the web reference to the Amazon web service. A web reference enables a project to consume one or more web services. It creates the proxy class needed to communicate with the web service. From the Project menu click Add Web References. The Add Web Reference dialog box opens. Enter the address to the web service in the address bar http://soap.amazon.com/schemas3/AmazonWebServices.wsdl (case sensitive). After you enter the URL, press Enter and the Amazon web service gets loaded and its information is shown in the dialog box as shown in figure 2. Moreover the Add Reference button will be enabled.
Figure 2: Adding Web Reference
Click the Add Reference button to add this web reference to our project. (If you don’t have Visual Studio .NET you will have to use command line wsdl.exe to generate the proxy class.)
Now in the Solution Explorer, expand the Web References folder to note the namespace (
com.amazon.soap) for the Amazon Web reference classes. For programming ease, right click on the web reference and rename it to Amazon.
Creating a web form
The first step to creating the Web form is to change the default name of the page from the WebForm1.aspx to AmazonService.aspx. Simply right click over the name in the Solution Explorer and choose Rename from the popup menu. Then you may enter the new name over the old name. Visual Studio.NET supports a drag and drop design tool that helps you layout your Web page quickly. In our case three
DataGrid and a button elements were dragged from the Web Forms toolbox on the left side of the screen onto the Web form and arranged. Modify the properties of the controls to reflect as below.
The final web form will look like that shown in the figure 3.
Figure 3: Web Form
You will have to enter keyword and mode from the user interface in order to do the search. Example of input parameters are keyword: Java and mode: books.
Invoking the Web service
Now we can proceed to write the logic to call the web service. The logic will reside in the button click method. The logic of button click is presented below:
Private Sub Search_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Search.Click
Dim TokenKey As String
Dim Service As Amazon.AmazonSearchService = New _
Dim keyword As Amazon.KeywordRequest = New Amazon.KeywordRequest()
Dim ProductInformation As Amazon.ProductInfo
TokenKey = "XXXXXXXXXXXXXXXX"
keyword.keyword = txtKeyword.Text keyword.mode = txtMode.Text keyword.page = 1 keyword.tag = "webservices-20" keyword.type = "lite" keyword.devtag = TokenKey
ProductInformation = Service.KeywordSearchRequest(keyword)
Datagrid1.DataSource = TransformToDataSet(ProductInformation)
Lets step through the above code. To make a SOAP request, create an instance of
AmazonSearchService. Next create the
KeyRequest instance for the
KeywordSearchRequest to be performed. Fill the properties of the
KeywordRequest object. The properties and descriptions are outlined below. The
mode property, the user will fill in through user interface.
|What you are searching for|
|Amazon product line like books, music.|
|An integer indicating the number of pages that the search request returns.|
|Set the type |
Heavy. Indicates the amount of XML data returned to you
Affiliate tag, if you don’t have one use webservices-20
|Your token information|
Finally invoke the
KeywordSearchRequest passing the
KeywordRequest object to it. The invocation returns
ProductInfo object. Then transfer the
ProductInfo information to a
DataSet. Finally bind the dataset to the
DataGrid so that information can be easily displayed to the user. Below code outlines how the returned
ProductInfo object information is transferred to
Function TransformToDataSet(ByVal productInfo As_
Amazon.ProductInfo) As DataSet
Dim ds As DataSet = New DataSet()
Dim detailsCollection() As Amazon.Details = _
Dim details As Amazon.Details
Dim datarow As DataRow
For Each details In detailsCollection
datarow = ds.Tables("ListedProducts").NewRow
datarow("Image") = details.ImageUrlSmall.ToString()
datarow("ProductName") = details.ProductName.ToString()
datarow("Authors") = details.Authors(0).ToString()
datarow("Price") = details.ListPrice.ToString()
Stepping through the above code we can see, that first we create the instance of
DataSet to hold the data. Then we construct the header column for the information we want to display to the user. We than message the
ProductInfo object and get an array of
Details object from it. The
Details object contains various properties about the product, the authors, the list price, image URLs (Check out the Amazon SDK documentation for details about the available properties). We than iterate through the collection of
Details objects and construct each
DataRow of the dataset. Additionally you would also want to put error handling when filling the
Displaying the result
Once the data is in the dataset it can be easily displayed to the user interface using the
DataGrid. Below outlined is the
DataGrid code of the user interface.
<asp:datagrid id=Datagrid1 AutoGenerateColumns="False"
CellPadding="4" BackColor="White" BorderWidth="1px"
BorderStyle="None" BorderColor="#C00000" runat="server">
<headERSTYLE BackColor="Olive" ForeColor="#C00000" Font-Bold="True">
<FOOTERSTYLE BackColor="#FFFFCC" ForeColor="#330099"></FOOTERSTYLE>
<asp:Image id=Image1 Runat="server" ImageUrl=
'<%# Container.DataItem("image") %>'></asp:Image>
<asp:BoundColumn HeaderText="Product" DataField="ProductName">
<asp:BoundColumn HeaderText="Author" DataField="Authors">
<asp:BoundColumn HeaderText="Price" DataField="Price">
Amazon web services are big and encapsulates various request objects like
AsinRequest and more. They allow you to programmatically access the majority of features on the Amazon site.