Use googleAPI through C #

Source: Internet
Author: User

· Preparations

To use googleAPI to develop your own desktop applications, make the following preparations:

1. First download the Google Web APIs Developer's Kit at the address http://www.google.com/apis/download.html.

2. Then in the following address https://www.google.com/accounts/NewAccount? Continue = http://api.google.com/createkey&followup=http://api.google.com/createkeyregister a license key to use the search service provided by google.

· Create a Google Search application

1. Create a Windows From project named Google Search.

2. Add a Web reference to use google web services. The specific method is to download the Google search in the Google Web APIs Developer's Kit. place the wsdl file on the local server, right-click the reference in resource manager in VS, select Add Web reference, and enter GoogleSearch. the address of the wsdl file on the local server: http: // localhost/GoogleSearch. wsdl. After changing the Web reference name to GoogleSearch, click Add reference to return.

3. Design the interface and add three controls, TextBox (used to enter keywords), Button (used to submit information), and RichTextBox (used to display search result information ).

4. Write event processing.

1) Add a namespace. Using Google_Search.googlesearch;

2) double-click the Button control and add the following code to the event processing of the Button:

Try
{
GoogleSearchService s = new GoogleSearchService (); // create a search service.
GoogleSearchResult r = s. doGoogleSearch ("NGYfW7dQFHKshnXPwvctLsaipk03YK2x", textBox1.Text, 0, 10, true, "", true, "", ""); // use the doGoogleSearch method to obtain the search result.
ResultElement [] re = r. resultElements; // input the result into a ResultElement array.
Foreach (ResultElement n in re)
{
RichTextBox1.AppendText (n. title + "\ r"); // display the title.
RichTextBox1.AppendText (n. snippet + "\ r"); // displays the summary containing keywords.
RichTextBox1.AppendText (n. URL); // display the webpage address.
RichTextBox1.AppendText ("\ n \ r ");
}
}

Catch (Exception ee)
{
MessageBox. Show (ee. Message );
}

3) press ctrl + F5 to test the application.

 

 

GoogleSearchServices doGoogleSearch Method

You need to create a GoogleSearchServices Class Object for search, and then call the doGoogleSearch method to obtain information. The following describes the method provided by google.

Public dogooglesearch (string key, string Q, int start, int maxresults, bool filter, string restrict, bool SafeSearch, string LR, string IE, string OE)

The following describes the parameters:

Key: This is an authentication ID provided by Google. You can apply for this ID at http://www.google.com/apis. Because it is currently a test phase, each ID provides only 1000 search service requests per day.

Q: This is the keyword to be searched. String type

Start: Number of indexes starting from the result, starting from 0. Int type

Maxresults: the maximum number of returned results. The maximum value is 10. Int type

Filter: used to identify whether to filter the search results. The filter only returns 1-2 results for the content on the same host. Bool type

Restrict: Specifies the country or region of the search. If it is null, it indicates no restriction. String type.

The following table lists all optional codes.

SafeSearch: uses a bool value to indicate whether to Filter Junk information.

LR: language restrictions, string type.

IE: Enter the character encoding used. This parameter will be ignored, and all input will be UTF-8.

OE: The character encoding used for output. This parameter will be ignored, and all output will use UTF-8

 

By setting the parameters of this method, your application is more user-friendly in search. You can use various controls to collect the information.

L googlesearchresult class

This class is used to store search results. The following describes some important attributes of this class:

1. Total number of estimatedTotalResultsCount information related to keywords.

2. The time when searchTime is used to submit information to return information.

3. searchTips provides keywords related to the keywords for selection.

4. resultElements returns an array to store each item of the returned result.

L Result Element class

This class refers to each item in the search result, and all results are processed by this class. The following describes some attributes of this class:

1. URL: the url of the result webpage.

2. snippet returns an excerpt from the target webpage containing a keyword segment.

3. title the title of the target webpage.

4. hostname this parameter displays the host name of the target webpage.

5. If the target webpage exists in the ODP directory, the summary is returned.

6. directoryTitle: displays the title of the ODP directory.

 

Through these two classes, you can obtain various query-related information. Through the above introduction, you can basically use all the functions provided by googleAPI.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.