Walkthrough Example Calling the eBay API Using Httppost from Visual Studio NET

This section contains a step-by-step example of using HTTP-POST to call the eBay Web service from Visual Studio .NET. In this example, you call the GetSearchResults operation to look up all eBay auction items in the Sandbox environment that have the word computer and are not priced higher than $1,000. You set up this example so that it can be used later in the chapter for the corresponding SOAP example as well.

1. Open Visual Studio .NET and select File O New O Project.

2. Select Visual Basic Project as the Project Type and Windows Application as the Template. For the project name, specify eBaySample, and for the path specify the location where you want the project to be created. Click OK to create the new project.

3. Use the toolbox to drag and drop two command buttons and one text box onto the form. Change the text property of the first text box to HTTP-POST Example, and change the text property of the second text box to SOAP Example. Change the multiline property of the text box to True and resize the text box so that it is large enough to display XML results. An example of how the form looks after these changes is shown in Figure 5-9.

Figure 5-9

4. Add the following procedure to Forml for the click event for the first button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Buttonl.Click

Dim web As New System.Net.WebClient

Dim strDevId As String Dim strAppId As String Dim strCertId As String

'specify credentials required to access API strDevId = "Your Dev Id Here" strAppId = "Your App Id Here" strCertId = "Your Cert Id Here"

'specify the configuration headers web.Headers.Add("X-EBAY-API-CALL-NAME", "GetSearchResults") web.Headers.Add("X-EBAY-API-SITEID", "0") web.Headers.Add("X-EBAY-API-DETAIL-LEVEL", "0") web.Headers.Add("X-EBAY-API-COMPATIBILITY-LEVEL", "311") web.Headers.Add("Content-Type", "text/xml")

'add required security headers for all API calls web.Headers.Add("X-EBAY-API-SESSION-CERTIFICATE", strDevId + ";" +

strAppId + ";" + strCertId) web.Headers.Add("X-EBAY-API-DEV-NAME", strDevId) web.Headers.Add("X-EBAY-API-APP-NAME", strAppId) web.Headers.Add("X-EBAY-API-CERT-NAME", strCertId)

Dim strXML As String

'list of items for sale that match search criteria strXML = "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _ "<request>" & _

"<RequestToken>Your Token Here</RequestToken>" & _ "<ErrorLevel>1</ErrorLevel>" & _ "<Verb>GetSearchResults</Verb>" & _ "<Query>computer</Query>" & _ "<HighestPrice>1000.00</HighestPrice>" & _ "<MaxResults>5</MaxResults>" & _ "<SiteId>0</SiteId>" & _ "</request>"

'add the xml string to the byte array

Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes(strXML)

'call the ebay api and pass the byte array containing the XML string Dim res As Byte() = _

web.UploadData("https://api.sandbox.ebay.com/ws/api.dll", "POST", d)

TextBoxl.Multiline = True

'display the results of the call in a text box on the form TextBoxl.Text = System.Text.Encoding.ASCII.GetString(res)

End Sub

5. Input your Dev ID, App ID, Cert ID, and Token on the lines of code where indicated. As I mentioned previously, these values are provided on the information screens you received when you set up your account.

6. Select File O Save All to save all changes to the project.

7. Select Debug O Start (or press F5) to run the project. Click the button that says HTTP-POST Example to run the HTTP-POST Example.

8. You should see a window similar to Figure 5-10 that displays the XML results returned from the eBay API. They begin with the first item that matched the search criteria.

You must use an XML parser to parse the values you use in your application in order to work with this in meaningful ways. eBay has recently announced a SOAP enhancement for many API operations that gives you the option of calling the API without having to parse XML in your underlying programs. Let's see how this works.

HE Forml _QiDQ

iTxml version=" 1 0" encoding="ijH-l' ?:-=;eE!ay:=

iSearch>

ittem^

i Id >4.5017D1557i/ld;

iTitle[CD ATA[New O'Sullivan Computer DeskJi>=/Title> ! SutititleTerf i ; I [CDATADI? i/SubtitleTejt > ■;Cjjrrenc:yld i 1 ^/Djrrencyld >

=: Link ><![CDATA[http://cgi.sandbcM.ebay.com/ws/eEayl SAPI.dll?Viewltenniitem=i5[}17D1557

4category=2049BIt-i'Link>

^Current Price 1=5. SEk/Cument Priced iLocalizedCurrent Price >$9.9&iyLocalizedCument Priced t.B IN Price >59S.S9=yB IN Price ;

^SiteCurTBncy^l i/SiteCurrency^

^Converted Price >S9.99°^Con verted Price >

Converted BIM Pnce !59S.99t/Coriverted BIN Price sBidCountiOi/BidCounti s Start Time >2DG4-10-22 12:37:49</5tatTn]e>

Figure 5-10

eBay Cash Insider

eBay Cash Insider

I refined my information, added notes as things came up and took out old or outdated information. Each time I discovered a new little tip or trick as I was making money with EBay, I added that information. Today, I bring home anywhere from 500 to 2000 each week with EBay. Thats not too shabby for someone who was just a year ago afraid to even purchase something from the site.

Get My Free Ebook


Post a comment