亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

怎樣在你的應用程序中集成Google搜索?

2019-11-18 19:24:43
字體:
來源:轉載
供稿:網友

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 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 搒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:


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抯 to filter the content for example to a specific country, language, topic, etc. Here are the arguments you pass along to the web method:


Key ?The Google license key.
QueryTerm ?The actual search term. This can be a simple word, a phrase (to search for the phrase you need to put it under double quotes otherwise it searches for the occurrence of all individual words), a list of words (you can use the AND or OR Operator; when no operator is used between the words AND is assumed), etc. You can also exclude words or phrases by putting a minus sign in front of it. The Google reference at http://www.google.ca/apis/reference.html explains all query term capabilities.
Start ?A zero based index of the first result to be returned. This allows you to page through the result set. The search result returned by this web method can not be more then MaxResults, therefore you need to make multiple calls and set Start appropriately to get the next results and so forth. If you provide a user interface which allows the user to page through the complete result set, then you would set Start accordingly, to return the results for each page. For example the first call would set it to 0, the next to 11, followed by 21, etc. (assuming MaxResults is set to 10).
MaxResults ?The maximum number of results to be returned by the query. This can be a value between one and ten.
Filter ?When set to true it filters out duplicate or near-duplicate search results. Near duplicate results are results with the same title and snippets (snippet is the summary text shown for each search result). This also limits the number of search results coming from the same host. So if a web site would return ten records matching the search term then this would only return the first two (called host crowding).
Restricts ?Allows to restrict the search to results from one or more countries or one or more topics. For example you can restrict the search to content within the US by setting this value to "countryUS". You can restrict the search to content centered around linux by setting this value to "linux". The Google reference at http://www.google.ca/apis/reference.html lists all the possible values.
SafeSearch ?Filters out adult content when set to true.
LanguageRestrict ?This allows you to restrict the search within one or more languages. The Google reference at http://www.google.ca/apis/reference.html lists all the possible values.
InputEncoding ?This value is ignored. All requests should be encoded using UTF-8.
OutputEncoding ?This value is ignored. All returned results are encoded using UTF-8.
This web method allows you to perform simple or complex search queries against Google. It also allows you to filter the search result as well as page through the search result. Here is a code snippet:


public static xmlNode Search(string QueryTerm, int Start, int
MaxResults, bool Filter, string Restricts,
  bool SafeSearch, string LanguageRestrict, string InputEncoding, string
OutputEncoding)
{
 // create an instance of the Google web service
 Google.GoogleSearchService GoogleService = new
Google.GoogleSearchService();

 // perform search
 Google.GoogleSearchResult SearchResult = GoogleService.doGoogleSearch(Key,
QueryTerm, Start,
  MaxResults, Filter, Restricts, SafeSearch, LanguageRestrict,
InputEncoding, OutputEncoding);

 // we return the result back as a XML document
 XmlDocument ResultXml = CreateXmlDocument(SearchResultXmlNode);

 // add the search result
 StringValueOfObject(ResultXml.DocumentElement, SearchResult);

 // add the result elements and directory categories root node
 XmlElement ResultElementsParentNode =
AddChildElement(ResultXml.DocumentElement, "ResultElements");
 XmlElement CategoriesParentNode =
AddChildElement(ResultXml.DocumentElement, "DirectoryCategories");

 // now add all result elements
 foreach (Google.ResultElement ResultElement in SearchResult.resultElements)
  StringValueOfObject(ResultElementsParentNode, ResultElement);

 // now add all directory categories
 foreach (Google.DirectoryCategory DirectoryCategory in
SearchResult.directoryCategories)
  StringValueOfObject(CategoriesParentNode, DirectoryCategory);

 // release the web service object
 GoogleService.Dispose();
 return ResultXml;
}
First we create an instance of the GoogleSearchService class and then we call the web method doGoogleSearch(). We pass along all the arguments as described above. This performs the search and returns its result as an instance of the GoogleSearchResult class. The code snippet then takes all values of the GoogleSearchResult object and puts them into a XML document. Please refer to the attached sample application for the complete code. First it creates a XML document with the method CreateXmlDocument(). It then calls the method StringValueOfObject() which creates a XML element for the object in the XML document using the name of the object as the name of the XML element. The method uses then reflection to walk the returned GoogleSearchResult object and for each field it finds in the object it adds an attribute to the created XML element. It of course adds to each created attribute the value of the associated object field. The returned GoogleSearchResult object has two fields which hold an array of ResultElement and DirectoryCategory objects. The method StringValueOfObject() is not able to walk each object in those arrays. Therefore we create two root XML elements in the XML document using the method AddChildElement(). We then loop through both arrays and call for each object StringValueOfObject() so we can convert each object to a XML element adding all its fields as attributes. Finally we call again Dispose() to free up the underlying unmanaged resources and then return the XML document which contains all search information of the GoogleSearchService object. This enables you to run XPath queries against the search result XML document to find the required search result information.


The attached sample application
The attached sample application provides a wrapper class for all Google web methods. It also provides a simple user interface demonstrating the use of each web method. You can enter a search term and get alternate spelling suggestions, you can download the cached HTML page of a URL and display it and you can perform a search entering all the search arguments. Please make sure to obtain your own Google license key and enter it in the app.config file.

Download Source


Summary
The Google web service API is very easy to use. It enables you to search the Internet from within your application. Complex query terms and filtering capabilities assure relevancy of the search results to your application needs. The Google web service is one of many other emerging ones, like Amazon's web service or eBay's web service. By introducing a web service interface these companies moved to a platform, enabling third parties to build solutions non top of them. For these companies an ever increasing number of requests and business transactions are coming through these web service interfaces. If you have comments on this article or this topic, please contact me @ klaus_salchner@hotmail.com . I want to hear if you learned something new. Contact me if you have questions about this topic or article.


About the author
Klaus Salchner has worked for 14 years in the industry, nine years in Europe and another five years in North America. As a Senior Enterprise Architect with solid experience in enterprise software development, Klaus spends considerable time on performance, scalability, availability, maintainability, globalization/localization and security. The projects he has been involved in are used by more than a million users in 50 countries on three continents.

Klaus calls Vancouver, British Columbia his home at the moment. His next big goal is doing the New York marathon in 2005. Klaus is interested in guest speaking opportunities or as an author for .NET magazines or Web sites. He can be contacted at klaus_salchner@hotmail.com or http://www.enterprise-minds.com/ .

Enterprise application architecture and design consulting services are available. If you want to hear more about it contact me! Involve me in your projects and I will make a difference for you. Contact me if you have an idea for an article or research project. Also contact me if you want to co-author an article or join future research projects!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美极品xxxx| 亚洲精品国精品久久99热一| 久久久这里只有精品视频| 亚洲欧美视频在线| 国产精品com| 精品亚洲永久免费精品| 日韩欧美国产骚| 久久偷看各类女兵18女厕嘘嘘| 欧美成人精品一区二区| 91精品国产沙发| 国产极品精品在线观看| 国产乱人伦真实精品视频| 国产日韩精品在线观看| 亚洲欧洲激情在线| 日韩av电影中文字幕| 亚洲免费av电影| 伊人久久久久久久久久| 亚洲一区二区少妇| 国产va免费精品高清在线| 日韩国产精品亚洲а∨天堂免| 红桃av永久久久| 精品国产91久久久久久老师| 欧美一级淫片aaaaaaa视频| 69久久夜色精品国产7777| 亚洲天堂男人天堂| 亚洲福利视频在线| 98精品在线视频| 亚洲自拍偷拍视频| 欧美电影免费观看电视剧大全| 亚洲国语精品自产拍在线观看| 精品国产美女在线| 日韩av电影手机在线观看| 另类视频在线观看| 91久久精品久久国产性色也91| 亚洲国产日韩欧美在线动漫| 黄色成人av在线| 91亚洲va在线va天堂va国| 黄网动漫久久久| 日韩欧美中文第一页| 性色av一区二区咪爱| 国内精品久久久久久久久| 国产精品入口夜色视频大尺度| 亚洲第一av网站| 日韩亚洲欧美成人| 日韩av在线看| 北条麻妃99精品青青久久| 日韩中文字幕视频在线| 久久久久久久久久国产精品| 国产91精品久| 欧美最猛性xxxxx(亚洲精品)| 亚洲福利视频在线| 亚洲美女激情视频| 国产精品精品视频| 黑人巨大精品欧美一区二区一视频| 一本大道香蕉久在线播放29| 在线播放精品一区二区三区| 国产精品久久久久久久久免费看| 亚洲xxx视频| 97久久精品国产| 97热精品视频官网| 亚洲自拍欧美另类| 自拍偷拍亚洲在线| 亚洲国产高清自拍| 2019最新中文字幕| 成人高清视频观看www| 国产一区二区日韩| 国产日韩欧美视频| 日韩电视剧免费观看网站| 国内偷自视频区视频综合| 日韩视频精品在线| 欧美日韩中国免费专区在线看| 国产精品久久久久久久久粉嫩av| 国产亚洲一区二区在线| 97**国产露脸精品国产| 中文字幕日本欧美| 国产日韩中文在线| 欧美日韩亚洲成人| 5278欧美一区二区三区| 亚洲精品videossex少妇| 日韩女在线观看| 欧美亚州一区二区三区| 黑人巨大精品欧美一区二区免费| 欧美激情国产精品| 欧美成人精品三级在线观看| 欧美激情精品久久久久久免费印度| 一区国产精品视频| 午夜免费日韩视频| 欧美激情性做爰免费视频| 亚洲系列中文字幕| 精品国产乱码久久久久酒店| 日韩电视剧免费观看网站| 日韩欧美国产免费播放| 亚洲自拍另类欧美丝袜| 91成人在线视频| 日韩av免费在线播放| 欧美精品性视频| 日韩中文在线不卡| 亚洲美女自拍视频| 4388成人网| 欧美激情精品久久久久久免费印度| 日韩毛片在线看| 在线观看国产成人av片| 久久久精品一区二区三区| 亚洲综合一区二区不卡| 91国产一区在线| 久久久久久国产三级电影| 亚洲国产成人爱av在线播放| 欧美男插女视频| 国产一区视频在线| 国产精品久久久久久久一区探花| 国产精品一区二区三| xxxx性欧美| 欧美日韩精品中文字幕| 57pao国产成人免费| 亚洲第一综合天堂另类专| 国色天香2019中文字幕在线观看| 岛国视频午夜一区免费在线观看| 乱亲女秽乱长久久久| 国产精品中文字幕在线| 欧美老女人性视频| 国产精品三级美女白浆呻吟| 国产成人精品免高潮费视频| 亚洲人高潮女人毛茸茸| 国产精品视频男人的天堂| 久久久久久久久国产| 成人激情视频网| 欧美成人精品一区二区| 草民午夜欧美限制a级福利片| xxxx欧美18另类的高清| 日韩一区二区av| 国产激情久久久久| 欧美性猛交xxxx免费看| 国产精品爱啪在线线免费观看| 亚洲精品美女久久久| 欧美激情亚洲精品| 久久av在线看| 欧美日韩亚洲视频| 成人激情在线观看| 超碰日本道色综合久久综合| 欧美第一黄网免费网站| 一区二区三区四区视频| 欧美中文字幕在线视频| 草民午夜欧美限制a级福利片| www.亚洲一二| 日本成人免费在线| 81精品国产乱码久久久久久| 亚州成人av在线| 日韩欧美在线播放| 国产欧美一区二区三区在线看| 91社影院在线观看| 久久91超碰青草是什么| 一区二区三区国产视频| 蜜臀久久99精品久久久无需会员| 欧美精品久久久久久久久久| 日韩在线视频网站| 91成人精品网站| 国产精品电影网| 精品动漫一区二区三区| 亚洲欧洲日产国产网站| 欧美一区二区三区精品电影| 国产欧美一区二区三区在线看| 一本一本久久a久久精品牛牛影视| 91精品国产自产在线老师啪| 国产精品视频精品|