|
4. Web
Services 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 this assignment requires an active
connection to the Internet.
To earn a passing grade
for this lab, you must demonstrate a working program to the instructor no later
than June 8.
Programming against Google's Web Service

1.
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).
2.
Remember
the security warning message you’ve been getting when creating projects on the
H: drive? Well, this application will
NOT run from the H: drive. Be sure to
create and run this application on the C: drive. When you are done, you will need to copy your
project back to your permanent storage space.
3.
Startup
Visual Studio and create a new C# Windows application. Set the location
bar to your directory on the C: drive.
Choose an appropriate name for your project.
4.
On
the form place
·
a text box for the search string,
·
a
label and textbox for displaying the estimated number of matches (should be
read only),
·
a
list box for displaying the resulting URLs - make sure the list box is pretty
wide since it will be used for displaying URLs,
·
a button for performing the search.
5.
Add
a web reference to your application to the Google web service.
·
In
the Solution Explorer window, right-click on References
·
Select
Add Web Reference
·
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
·
Click
on Go.
·
When
the com.google.api shows up in the Web reference name
box, click the Add Reference box.
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.
6.
Program
the search button (double click on the control to get from the control to its
underlying code).
·
Declare
two local reference variables, one of type GoogleSearchResult and another of type GoogleSearchService. Note that by default, you'll
need to reference these classes using their namespace "com.google.api".
com.google.api.GoogleSearchResult result;
com.google.api.GoogleSearchService google;
·
Next,
create a new instance of GoogleSearchService.
·
Finally,
call the doGoogleSearch() method using this instance and assign the result to your
local GoogleSearchResult variable.
result = google.doGoogleSearch("license key
string",
"search string", 0, 3,
false, "", false, "",
"", "");
·
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 either of my
own personal license keys:
YN0swmlQFHJWWESzD6kKRxSBfBGxHNP0
ICYk3yFQFHK1S8NLFb20d1CI4KqswxmI
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 activating your account, go to http://www.google.com/apis/ and follow
the link to Create Accounts. Sign in and
a copy of your google license key will be sent to
your email address.
·
The
second parameter is the search string.
Initially hard code in a word or phrase you wish to search on.
·
The
third parameter is 0 and the fourth is 3.
These ask Google to return the first 3 hits, numbered 0
.. 2. (Ten is the maximum number
of matches you can request at once).
·
The
rest of the parameters should match the pattern given above.
·
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.
·
Now
rebuild and run! Remember to click the
Search button.
7.
Once
that works, the next step is to display the first 3 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. Using a foreach statement, loop through the resultElements
array, displaying each URL in the list box.
foreach (com.google.api.ResultElement
r in result.resultElements)
lstHits.Items.Add
( r.URL );
8.
Now
run! Wow!
9.
Change
the second parameter in the call to doGoogleSearch so
the search string is taken from the contents of the text box. Type in a search term and run.
10.
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 3 to get matches 3 ..
5, then pass 6 to get matches 6 .. 8,
and so on. Write a simple loop to get the first 20 or so matches, and
display these in the list box. Run, and be amazed :-)
11.
DEMONSTRATE THIS SOLUTION NOW OR SAVE A COPY OF THIS
VERSION AND DEMONSTRATE IT LATER
12.
How
can you learn about all this? Surf to http://www.google.com/apis/ for more
information, API documentation, sample .NET code, etc. Enjoy!