怎样在您的应用程式中集成google搜索?_c#应用

2008-02-23 05:44:48来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

How to Integrate Google Searches into Your Application
By Klaus Salchner www.csharphelp.com


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 Googles 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 developers 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 搒ign 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 id 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抯 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抯 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:

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: hfc反向通道的调整(上)_视频通信

下一篇: 关于c#中枚举打印机_c#应用