|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWeb 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 startedOpen 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 ReferenceNow 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 ( Creating a web formThe 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
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 serviceNow 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
Finally invoke the 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 Displaying the resultOnce the data is in the dataset it can be easily displayed to the user interface using the <?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>
ConclusionAmazon web services are big and encapsulates various request objects like
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||