Implementing Amazon Web Services in VB.NET






4.59/5 (17 votes)
Sep 11, 2003
5 min read

146869

1053
Article shows implementing Amazon web service in VB.NET.
Introduction
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.
Getting started
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 Label
, two TextBox
, one 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.
Control | ID | Text |
Label1 |
lblKeywordSearch |
Keyword Search |
Label2 |
LblKeyword |
Keyword |
Label3 |
LblMode |
Mode |
Textbox1 |
TxtKeyword |
|
Textbox2 |
TxtMode |
|
Button |
Search |
Search |
DataGrid |
DataGrid1 |
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
' Variable to Store the Token Key
Dim TokenKey As String
' Declare variable for the amazon search service object
Dim Service As Amazon.AmazonSearchService = New _
Amazon.AmazonSearchService()
'Declare variable for KeywordRequest which is the object
'for the search request
Dim keyword As Amazon.KeywordRequest = New Amazon.KeywordRequest()
' Declare variable for the amazon Search Result
Dim ProductInformation As Amazon.ProductInfo
' Please Type your token key here
TokenKey = "XXXXXXXXXXXXXXXX"
'Fill in the properties of the keyword request
keyword.keyword = txtKeyword.Text 'what you are searching
keyword.mode = txtMode.Text 'amozon product line
keyword.page = 1 'page you want
keyword.tag = "webservices-20" 'affiliate tag
keyword.type = "lite" 'lite or heavy type
keyword.devtag = TokenKey 'token key
'Initiate the search
ProductInformation = Service.KeywordSearchRequest(keyword)
'Transform productInformation to DataSet
Datagrid1.DataSource = TransformToDataSet(ProductInformation)
Datagrid1.DataBind()
End Sub
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 keyword
and mode
property, the user will fill in through user interface.
Properties | Description |
Keyword |
What you are searching for |
Mode |
Amazon product line like books, music. |
Page |
An integer indicating the number of pages that the search request returns. |
Type |
Set the type Lite or Heavy . Indicates the amount of XML data returned to you |
Affiliate Tag |
Your Affiliate tag, if you don’t have one use webservices-20 |
Dev-tag |
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 DataSet
.
Function TransformToDataSet(ByVal productInfo As_
Amazon.ProductInfo) As DataSet
'Create the instance of dataset
Dim ds As DataSet = New DataSet()
'Create the headers
ds.Tables.Add("ListedProducts")
ds.Tables(0).Columns.Add("Image")
ds.Tables(0).Columns.Add("ProductName")
ds.Tables(0).Columns.Add("Authors")
ds.Tables(0).Columns.Add("Price")
'Get the Details object collection from
'productInfo object
Dim detailsCollection() As Amazon.Details = _
productInfo.Details
'Declare variables to be used in iteration
Dim details As Amazon.Details
Dim datarow As DataRow
'Iterate through detail object and
'get its properties
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()
ds.Tables("ListedProducts").Rows.Add(datarow)
Next
Return ds
End Function
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 DataSet
.
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.
<?xml:namespace prefix = asp />
<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">
</headERSTYLE>
<FOOTERSTYLE BackColor="#FFFFCC" ForeColor="#330099"></FOOTERSTYLE>
<COLUMNS>
<asp:TemplateColumn>
<asp:Image id=Image1 Runat="server" ImageUrl=
'<%# Container.DataItem("image") %>'></asp:Image>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Product" DataField="ProductName">
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Author" DataField="Authors">
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Price" DataField="Price">
</asp:BoundColumn>
</COLUMNS>
</asp:datagrid>
Conclusion
Amazon web services are big and encapsulates various request objects like KeywordRequest
, AuthorRequest
, AsinRequest
and more. They allow you to programmatically access the majority of features on the Amazon site.