6.Add the following SubMainprocedure to Module1of your project:

Figure 2-910.Click Enter in the console window to end the application. The essence of this example is included in the SubMainprocedure, so let s walk through it in furtherdetail to see how it works. First, you created a new Google Search Service. Create a Google Search objectDim Search As New Google.GoogleSearchServiceNext, you declared various variables to hold the parameters to pass to the search query. Dim strLicense As StringDim strSearchText As StringDim intStart As IntegerDim intMaxResults As IntegerYou then assigned the values to the search variables, such as the developer key, the search string, thestarting position for the query, and the maximum number of results to return. set the values to pass to the Google web servicestrLicense = YOUR KEY GOES HERE strSearchText = web service API intStart = 0intMaxResults = 3You then executed the doGoogleSearchmethod and passed the search variables defined previously. Try execute the doGoogleSearch method (i.e. run the Google Search) Dim SearchResult As Google.GoogleSearchResult = _ Search.doGoogleSearch(strLicense, strSearchText, _ intStart, intMaxResults, False, , False, , , ) 28Chapter

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Inexpensive Web Hosting services

6.Add the following SubMainprocedure to Module1of your project:

6.Add the following SubMainprocedure to Module1of your project: Sub Main() Create a Google Search objectDim Search As New Google.GoogleSearchServiceDim strLicense As StringDim strSearchText As StringDim intStart As IntegerDim intMaxResults As Integer set the values to pass to the Google web servicestrLicense = YOUR KEY GOES HERE strSearchText = web service API intStart = 0intMaxResults = 3Try execute the doGoogleSearch method (i.e. run the Google Search) Dim SearchResult As Google.GoogleSearchResult = _ Search.doGoogleSearch(strLicense, strSearchText, _ intStart, intMaxResults, False, , False, , , ) if no results were returned then exitIf SearchResult.resultElements Is Nothing Then Exit Sub write the results to the consoleDim result As Google.ResultElementFor Each result In SearchResult.resultElementsConsole.WriteLine() Console.WriteLine(result.title) Console.WriteLine(result.URL) Console.WriteLine(result.snippet) Console.WriteLine() Next get input from the user so display will pause until enter pressedConsole.ReadLine() Catch exception As System.Web.Services.Protocols.SoapExceptionMsgBox(exception.Message) End TryEnd Sub7.Input your developer license key on the line of code that says YOURKEYGOESHERE. 8.Select File.Save All to save all changes to the project. 9.Select Debug.Start (or press F5) to run the project. You should see a console window similarto Figure 2-9 that retrieves information from the Google API based on the criteria specified code.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Inexpensive Web Hosting services

Cache RequestsAnother method supported by the Google API

Figure 2-74.Click the Add Reference button to add the reference to your project. Visual Studio .NET uses theWSDLfile to identify the methods available for execution from your project. 5.In the Solution Explorer of the project, navigate to the Web references node and select theGoogle Web service that was added. Modify the properties of the Folder Name for the Web service to Google so that you can use a shorter name in your code. The results of this modifica- tion are shown in Figure 2-8. Figure 2-826Chapter

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Adult Web Hosting services

Cache RequestsAnother method supported by the Google API

Cache RequestsAnother method supported by the Google API is the doGetCachedPagemethod. This method allowsyou to retrieve the cached version of a URLfrom the Google cache. The doGetCachedPagemethodreturns a byte array and has the following syntax: bytPage() = Search.doGetCachedPage(Key, URL) .Keyis the developer key you were provided by Google. .URLis the URLof the site you wish to retrieve from the Google cache. Spelling RequestsAthird method supported by the Google API is the doSpellingSuggestionmethod. This methodallows you to retrieve a spelling suggestion/correction for a particular search phrase. ThedoSpellingSuggestionmethod returns a string and has the following syntax: strSuggestedSpelling = Search.doSpellingSuggestion(Key, Phrase) .Keyis the developer key you were provided by Google. .Phraseis the text phrase for which you would like to receive a spelling suggestion. The details of how to call each of these three methods are explained later in this chapter. Now that the basic idea of the types of queries that can be executed using the Google API, let s look at anexample of how to execute a search request. Executing a QueryIn this section, I walk you through examples that illustrate how to execute a Google API query usingVisual Studio .NET as well as directly from the .NET Framework. Executing a Query Using Visual Studio .NET1.Open Visual Studio .NET and select File.New.Project. 2.Select Visual Basic Project as the Project Type, and select Console Application as the Template. For the project name, specify GoogleSearchConsoleor another suitable name. For the pathspecify the location where you want the project to be created. Click OK to create the new project. 3.Add a reference to the Google API by selecting Project.Add Web Reference. For the URLfield, specify http://api.google.com/GoogleSearch.wsdl. You may recall from Chapter the WSDLfile of a Web service explains the features that are available in the service. Clickthe GO button so that Visual Studio can locate the Google service. Ascreen similar to thatshown in Figure 2-7 is displayed.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Adult Web Hosting services

Figure 2-6Anatomy of a Google API QueryIn this

Syntax of a Google Search RequestThe doGoogleSearchmethod enables you to execute a Google search request. Atypical Google APIsearch request using doGoogleSearchis structured as follows: SearchResult = Search.doGoogleSearch(Key, q, _ Start, MaxResults, Filter, Restrict, SafeSearch, lr, ie, oe) .Keyis the developer key that Google provided for you. .qis the search string text of your search command, which can be just a simple search or caninclude the advanced options reviewed earlier in this chapter. .Start is the starting index where you want the results to start. You should use 0 if you wantthe first set of results. .MaxResultsis the maximum number of results to return. It can be a value from 1 to 10. .Filteris a Boolean value that specifies whether your results go through automatic query filtering. .Restrictis a text value that will restrict your search to a specific country or one of Google stopical searches. This field is usually left blank to leave the search unrestricted. .SafeSearchis a Boolean value that specifies whether the results will be filtered to removeadult content. .lrstands for language restrictand allows you to limit your search results to a particular language. .iestands for input encoding and allows you to specify the type of character encoding that yourquery will use. Google s documentation says that request data should use the UTF-8 format. You can leave this field blank. .oestands for output encoding, which should be UTF-8. You can leave this field blank. Syntax of a Google Search ResponseThe most important aspects of the search response are returned in a resultElementslist that containselements such as the following: .

Figure 2-6Anatomy of a Google API QueryIn this

Figure 2-6Anatomy of a Google API QueryIn this section, you explore the structure of a Google API query and learn the basics of how to execute and work with the results. Query SyntaxThe Google API supports search requests, retrieving pages from the Google cache, and spelling sugges- tions. Let s now look at each of these in greater detail. Search RequestsIn addition to simple keyword searches, the Google API supports the following search options: allintext: allinlinks: cache: daterange: filetype: info: intitle: inURL: link: related: site:

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services

Figure 2-4The Developer s Kit contains several code examples

Figure 2-5To create a Google account, you are prompted to specify an e-mail address, a password, and a passwordconfirmation. You are also prompted to type the word verification. After clicking the button to proceed, you receive a confirmation screen similar to the one shown in Figure 2-6 informing you that your keyhas been sent by e-mail to the address of your account. Open the e-mail and copy the developer key to a place where you can easily access it. Later, you willneed the developer key for all calls to the Google API. 22Chapter

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Adult Web Hosting services

Figure 2-4The Developer s Kit contains several code examples

Figure 2-4The Developer s Kit contains several code examples that illustrate how to call the Google API from ous programming languages, including .NET. After downloading the developer s kit to your hard you create a Google account. When you click the link to create a Google account, you see a screen similarto the one shown in Figure 2-5.21Using

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Adult Web Hosting services

link: This option returns a list of Web

Figure 2-3You are provided with step-by-step instructions for how to sign up for and use the Google API. First, you are told to download the developer s kit. When you click the link to download the Google API, youare presented with a license agreement (similar to the one shown in Figure 2-4) that you must acceptbefore the file can be downloaded. You select the Agree option and then click the Download Now buttonto start the download. 20Chapter

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services

link: This option returns a list of Web

link: This option returns a list of Web pages that link to the specified URL. You do not need to include thehttp:// syntax. link:www.intel.comphonebook: This option allows you to look up a phone number. phonebook:317-634-3456related: This option allows you to retrieve a list of pages that are related to the one you specified. This might for example, to find alternative sites to search in the same category. related:www.google.comsite: This option allows you to limit your search to a site or a domain. site:gov grantssite:uspto.gov utility patentGoogle provides numerous other advanced features, such as spelling, language translation, and more. Now that you have briefly reviewed some features offered by Google, it s time to turn to the Google Introduction to the Google APIYou should recall from Chapter 1 that you can call an XMLWeb service in at least three different ways using SOAPwith HTTP, using HTTP-GET (REST), and using HTTP-POST. The Google API is currentlyavailable using SOAPwith the HTTPprotocol. At the time of this writing, the Google API was in beta. Therefore, the examples in this chapter are written using the beta version. Signing Up and Obtaining a KeyBefore you can use the Google API, you must first obtain a developer registration key. Although obtain- ing and using the developer key is free, Google currently enforces a limitation of 1,000 queries per oper per day. To sign up to use the Google API and to obtain a developer registration key, go towww.google.com/apis/as illustrated in Figure 2-3.19Using

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Clan Web Hosting services