[ Team LiB ] Previous Section Next Section

Hack 87 Program AWS with SOAP and VB.NET

figs/expert.giffigs/hack87.gif

VB.NET was made for Web Services. With some WSDL magic, much of the code is generated for you.

This little application isn't particularly fascinating, but the code here can be used as a foundation for more complex VB.NET applications that use Amazon Web Services. The basic methods of creating a SOAP proxy and the syntax for making the request can be used no matter how simple or complex the application.

WSDL (Web Service Description Language) files can allow Web Services to describe what features and functions are available. Software can examine the WSDL file and automatically generate the code necessary to access it. This is exactly what the .NET framework can do. By supplying the Amazon Web Services WSDL file, much of the work is done for you!

87.1 What You Need

First, make sure you have the .NET framework and the .NET SDK, both freely available at http://msdn.microsoft.com/library/default.asp?url=/downloads/list/netdevframework.asp. You could also create the program in Visual Studio if you have it available, but it's not necessary.

Next, create the SOAP client proxy. The wsdl.exe program examines a WSDL file and generates the code to create the class. You run it on the command line like this, providing the latest WSDL file:

wsdl.exe /l:vb http://soap.amazon.com/schemas2/AmazonWebServices.wsdl

This creates the file AmazonWebServices.vb.

To get a sense of what this program is working with, bring up the WSDL file in a browser. You'll see definitions for all of the possible SOAP requests and the parameters they accept. It's surprisingly easy to understand once you spend some time working with them; in fact, it's often easier to check the WSDL file instead of the documentation when you want to find which parameters you need to send for a specific request.

87.2 The Code

Create a text file called Amazon.vb and include the following code:

Imports System
Module Amazon
Sub Main(ByVal args As String(  ))
    'Your AWS developer's token
    Dim devToken As String ="insert developer token"

    'Your Affiliate Code
    Dim afTag As String ="insert associate tag"

    'Take the query from the command-line
    If args.Length <>1 Then
        Console.WriteLine("Usage:amazon.exe <keyword>")
        Return
    End If
    Dim kword As String = args(0)

    Dim amazonSearch As AmazonSearchService = New AmazonSearchService(  )

    Dim KeywordReq as new KeywordRequest(  )
    KeywordReq.keyword = kword
    KeywordReq.type = "lite"
    KeywordReq.tag= afTag
    KeywordReq.devtag = devToken
    KeywordReq.mode = "books"

'Query Amazon
Dim results As ProductInfo = amazonSearch.KeywordSearchRequest(KeywordReq)

    'results?
    If results.Details Is Nothing Then Return
    
    'Loop through results
    Dim result As Details
    For Each result In results.Details
        Console.WriteLine(  )
        Console.WriteLine(result.ProductName)
        Console.WriteLine("ASIN: " & result.ASIN)
        Console.WriteLine(  )
    Next
End Sub
End Module

Once both the proxy code (AmazonWebServices.vb) and the search code (Amazon.vb) are ready to go, you can compile the program. It's a good idea to explicitly include references to the files (assemblies) that contain code that the program needs. You can specify these with the /r:[file name] switch.

vbc /out:amazon.exe /r:System.dll /r:System.Web.Services.dll [RETURN]
/r:System.xml.dll *.vb

87.3 Running the Hack

The program is called from the command line, followed by the keyword you'd like to look for:

amazon.exe hacks

You should see several lines returned that look something like this:

Google Hacks
ASIN: 0596004478

Mac OS X Hacks
ASIN: 0596004605

To search for a phrase instead of a single word, enclose it in quotes.

    [ Team LiB ] Previous Section Next Section