Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / Visual Basic
Article

Implementing Amazon Web Services in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.59/5 (18 votes)
10 Sep 20035 min read 145.3K   1.1K   39   16
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.

Image 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.

Image 2

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.

ControlIDText
Label1lblKeywordSearchKeyword Search
Label2LblKeywordKeyword
Label3LblModeMode
Textbox1TxtKeyword 
Textbox2TxtMode 
ButtonSearchSearch
DataGridDataGrid1 

The final web form will look like that shown in the figure 3.

Image 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:

VB
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.

PropertiesDescription
KeywordWhat you are searching for
ModeAmazon product line like books, music.
PageAn integer indicating the number of pages that the search request returns.
TypeSet the type Lite or Heavy. Indicates the amount of XML data returned to you
Affiliate TagYour Affiliate tag, if you don’t have one use webservices-20
Dev-tagYour 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.

VB
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.

ASP.NET
<?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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Architects softwares with emerging technologies that will save time and increase productivity.

Comments and Discussions

 
Suggestionerror while adding web reference... Pin
Santosh K. Tripathi1-May-14 20:51
professionalSantosh K. Tripathi1-May-14 20:51 
GeneralHTTP Error Pin
kamran8531-Jul-09 22:39
kamran8531-Jul-09 22:39 
GeneralRe: HTTP Error Pin
mrrcomp16-Sep-09 10:15
mrrcomp16-Sep-09 10:15 
Questiondataset not defined????? Pin
pendo14-Apr-09 1:20
pendo14-Apr-09 1:20 
AnswerRe: dataset not defined????? Pin
mjk123451-Oct-09 14:47
mjk123451-Oct-09 14:47 
GeneralAmazone Pin
keyur soni23-Jun-08 0:20
keyur soni23-Jun-08 0:20 
it give me erro this
so tell me the another webservice url
The request failed with HTTP status 410: Gone.
GeneralRe: Amazone Pin
kamran8531-Jul-09 22:43
kamran8531-Jul-09 22:43 
GeneralThere is a problem for this webService Pin
Ayman Alsayed23-Aug-07 0:56
Ayman Alsayed23-Aug-07 0:56 
GeneralVery Useful Pin
jsreekumar4-Aug-07 23:32
jsreekumar4-Aug-07 23:32 
GeneralThe remote name could not be resolved Pin
Lets explore it20-Jun-06 20:59
Lets explore it20-Jun-06 20:59 
Generalerror with DataGrid asp:Image tag Pin
Beetlebub23-May-05 8:09
Beetlebub23-May-05 8:09 
QuestionHow to sort by Item Condition Pin
Sumit Kapoor13-Jun-04 20:21
Sumit Kapoor13-Jun-04 20:21 
GeneralServer Error in '/amazonproject' Pin
grobb14-Jun-04 23:07
grobb14-Jun-04 23:07 
GeneralDisplay more search results/Search by ISBN Pin
dcmurray18-Feb-04 14:31
dcmurray18-Feb-04 14:31 
QuestionWhere to put DataGrid Code? Pin
MReinbold18-Nov-03 9:22
MReinbold18-Nov-03 9:22 
AnswerRe: Where to put DataGrid Code? Pin
Pratik Desai21-Nov-03 6:39
Pratik Desai21-Nov-03 6:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.