6. WebServices Lab |
Overview: you will create a WinForms client application that communicates with Google's web service to perform searches and display the results. Note that IIS must be installed for this lab assignment; the assignment also requires an active connection to the internet.
Programming against Google's Web Service
This is a great example of the power of web services. Google.com has a free web service (up to 1000 searches per day) that allows you to perform Google-based searches and manipulate / display the results any way you want. In essence, you submit a query to Google, and you get back an array of result objects, each object containing a URL (among other things). If you want to see the application in action before you code it, there is a solution provided in the solution\GoogleApp directory: run the .EXE from the bin\Debug sub-directory.
Startup Visual Studio and create a new C# Windows application. On the form place a text box for the search string, a label for displaying the estimated number of matches, a list box for displaying the resulting URLs, and a button for performing the search. Make sure the list box is pretty wide since it will be used for displaying URLs.
Add a web reference to your application to the Google web service. Right-click on your project references, select web reference, and in the dialog box that appears, type the following URL to gain access to the necessary web service description file (WSDL): http://api.google.com/GoogleSearch.wsdl. Once added, the web service classes will exist within the namespace "com.google.api"; this should be the name you see in the list of Web References for your project.
Program the search button to declare two local reference variables, one of type GoogleSearchResult and another of type GoogleSearchService (remember, these are declared within the namespace com.google.api, and unfortunately you cannot import web-based namespaces with a "using" directive). Next, create an instance of GoogleSearchService. Finally, call the doGoogleSearch() method using this instance and assign the result to your local GoogleSearchResult variable. For now, code the first parameter as "", the second parameter is the search string (which you should take from the contents of the text box), the third parameter is 0 and the fourth is 10 (these ask Google to return the first 10 hits, numbered 0 .. 9, where 10 is the maximum number of matches you can request at once). For the remaining parameters, pass "" for string and false for boolean. Don't try to run yet, there's the issue of a license key...
The first parameter of the doGoogleSearch() method must be a non-empty string containing a valid license key. For the purposes of this lab, feel free to use my own personal license key:
4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H
However, in the future, if you plan to work with Google's web service, please register for a free Google account and obtain your own personal license key. You can register for an account at: https://www.google.com/accounts/CreateAccount.
After the call to doGoogleSearch(), an object is returned of type GoogleSearchResult. This object contains a field called estimatedTotalResultsCount, which denotes the estimated number of hits based on your search string. Convert this value to a string and display it in your label. Finally, display a message box telling the user that the search is complete. Now run!
Once that works, the next step is to display the first 10 hits in your list box. The GoogleSearchResult object contains a field called resultElements, which is an array of objects of type ResultElement. Each ResultElement object contains a URL. Loop through the resultElements array, displaying each URL in the list box. Now run! Wow!
If you want to display more URLs, you need to make additional calls to Google via the doGoogleSearch() method. All you need to do is change the 3rd parameter each time: pass 10 to get matches 10 .. 20, then pass 20 to get matches 20 .. 30, and so on. Write a simple loop to get the first 100 matches, and display these in the list box. Run, and be amazed :-)
How did I learn about all this? Surf to http://www.google.com/apis/ for more information, API documentation, sample .NET code, etc. Enjoy!
If time permits...
Add a web browser control to your form, and if the user clicks on an item in the list box, navigate to this URL... First you'll need to add a web browser control to your toolbox: right-click on the toolbox, select customize, COM tab, and then browse to the System32 directory within the C:\Windows (or C:\WinNT) directory. Look for the file "shdocvw.dll", select it, and click Open. It should now be checked in the Customize Toolbox window, and so click OK. Scroll to the bottom of your toolbox and you should see an Explorer icon. Drag-and-drop one on your form. Now program the form so that when the list box's SelectedIndexChanged event occurs, you call the web browser control's (whose name should be axWebBrowser1) Navigate() method. The first parameter is the URL, the remaining 4 parameters are optional: declare 4 local variables of type object, set them to null, and then pass them by-reference to the Navigate() method. Run, and be even more amazed :-)
That's it!