Introduction
The first thing coming to mind when we hear Google is search engine. Google has been able to turn the search business up-side-down within the last five years. The founders of Google started with an idea in 95 which really became widely used and known in 98/99. Today Google is the number one search engine. You can find out more about Google's history here. Like other organizations Google is trying to establish itself as a platform rather then a solution. This means it provides the necessary tools and infrastructure so other people can build their own solutions on top of it. Google provides a web service interface which allows you to integrate Google searches right into your application. You can find out more about the Google web service API at http://www.google.ca/apis
How to get started with the Google API
You can download from the URL above the developer's kit which comes with a number of sample applications for different languages like .NET or Java. You also need a valid license key, which you need to pass along with every web service call. To obtain a Google license key visit the URL http://www.google.ca/apis and select "Create Account" on the left side navigation bar. You need to create an account by entering your email address and a password. This sends an email to the email address you entered to verify its existence. The email you receive has a link to complete the account creation by activating it. When done click on the continue link which brings you back to the account creation page. At the bottom of the page you see a link "sign in here". Follow the link and sign into your account with your email address and password. This shows then a page confirming that a license key has been generated and sent to your email address. Should you loose your license key, sign in again and Google will resend the license key to your email address. The license key is for free but limits you to 1,000 calls per day. This will be more then enough to get started. If you need to make more then 1,000 calls per day contact Google.
How to reference the Google web service API in your project
Create your project in Visual Studio .NET and in the "solution explorer" pane right click on the project. In the popup menu select "Add Web Reference" and enter as URL the following WSDL URL - http://api.google.com/GoogleSearch.wsdl. This will check the existence of the WSDL, download it and show you in the dialog the web methods available by this web service. Enter under "web reference name" the name of the web service reference, for example GoogleSearch. When done click "Add Reference" and you are ready to use the Google web service API. It will be shown in the "solution explorer" under "Web References". You can right click on the web service reference and update it through the "Update Web Reference" menu item or view it in the object explorer through the "View in Object Browser" popup menu. This shows you that there are four different types available. The type GoogleSearchService exposes the actual web service calls you can make. It has three different web methods (plus the usual Begin/End methods if you want to call a web method asynchronously).
GoogleSearchService.doSpellingSuggestion()
When you open up Google in your browser and search for a word or phrase you see sometimes the phrase "Did you mean: [suggested search term]" at the top of the search results page. Google performs a spell check of the search term you entered and then shows you alternative spellings of your search term. This helps the user to search for properly spelled words and phrases and the user can simply click on it to search for the corrected search term. The Google web service also provides a web method to check for alternate spellings of a search term. Here is a code snippet:
public static string SpellingSuggestion(string Phrase)
{
// create an instance of the Google web service
Google.GoogleSearchService GoogleService = new Google.GoogleSearchService();
// get the new spelling suggestion
string SpellingSuggestion = GoogleService.doSpellingSuggestion(Key, Phrase);
// null means we have no spelling suggestion
if (SpellingSuggestion == null)
SpellingSuggestion = Phrase;
// release the web service object
GoogleService.Dispose();
return SpellingSuggestion;
}
First we create an instance of the web GoogleSearchService class and then we call the web method doSpellingSuggestion(). The first argument is the Google license key you pass along and the second one is the search term. The web method returns the alternate spelling of the search term or null if there is no alternate spelling. The code snippet above returns the alternate spelling or the original one. At the end it calls Dispose() to free up the underlying unmanaged resource.
GoogleSearchService.doGetCachedPage()
Google is constantly crawling the Internet to keep its search index and directory up to date. Google's crawler also caches the content locally on its servers and allows you to obtain the cached page, which is the content as of when the crawler visited that resource the last time. URL's can point to many different resources, most typically to HTML pages. But these can also be Word documents, PDF files, PowerPoint slides, etc. The cached page is always in HTML format. So for any other resources then HTML it also converts the format to HTML. Here is a code snippet:
public static void GetCachedPageAndSaveToFile(string PageUrl, string FileName)
{
// create an instance of the Google web service
Google.GoogleSearchService GoogleService = new Google.GoogleSearchService();
// get the cached page content
byte[] CachedPage = GoogleService.doGetCachedPage(Key, PageUrl);
// file writer to write a stream to the file & a binary writer to write data to
FileStream FileWriter = new FileStream(FileName, FileMode.Create);
BinaryWriter Writer = new BinaryWriter(FileWriter);
// write the page content to the file and close the streams;
Writer.Write(CachedPage);
Writer.Close();
FileWriter.Close();
// release the web service object
GoogleService.Dispose();
}
First we again create an instance of the GoogleSearchService class and then we call the web method doGetCachedPage(). We pass along the Google license key plus the URL of the page we are looking for. This returns a byte array, using base64 encoding, which contains the HTML content of the cached page. Next we create a FileStream which we use to write the obtained page to a local file. With FileMode.Create we tell it to create the file, which overwrites any existing file. Then we create a BinaryWriter which uses as output the FileStream. Then we write the returned byte array to the BinaryWriter which in turn writes it to the FileStream, which in turn writes it to the local file. Then we close the FileStream and BinaryWriter. At the end we call again Dispose() to free up underlying unmanaged resources.
GoogleSearchService.doGoogleSearch()
The web method doGoogleSearch() allows you to perform searches. You pass along the search term and then certain filter criteria's to filter the content for example to a specific country, language, topic, etc. Here are the arguments you pass along to the web method:
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Gowtham RajamanickamPosted Apr 25, 2015, 6:35 AM
good
HiralPosted Mar 25, 2013, 5:23 PM
give the link page is not found