Yesterday I wrote about Google's translation API and showed how you can build a simple class structure to consume that web service.
I got caught by the other REST services offered by Google in connection with their AJAX Search API, so I wrote up a project to consume the various search services. You can find the C# source files here. The project makes it possible for you to perform Google searches from your web server or, even better, in your Silverlight project (I haven't actually checked Google's cross domain policy, but hopefully they are going to support it).
The core of the project are the various SearchResponse classes. One for each search type since they all have differing data properties. These classes are used in the generic class AjaxSearch which will return a generic ResultSet for the various searches. The ResultSet class not only holds a generic list of the SearchResponses but also a ResponseCursor which holds meta data about the search.
To perform a search all you need to do is call:
C#
ResultSet<WebSearchResult> mySearch = AjaxSearch.WebSearch(
"http://www.mydomain.com",
"Reimers.dk Google API",
SafetyFiltering.Off,
"MyGoogleDeveloperKey",
CultureInfo.CurrentCulture,
ResultSize.Large);
The search method creates the request URL and passes it to the private DoSearch method which performs the actual HttpWebRequest and deserializes the response using a JavaScriptSerializer.
One thing that is missing from the project in its present state is result paging.
You are free to use the source code in your private or commercial projects, but please don't forget to mention where you got the code from.