要使用 googleAPI 来开发自己的桌面应用程序,先要做下面的准备工作:
1. 首先在下面地址 http://www.google.com/apis/download.html 下载 the Google Web APIs Developer's Kit 。
2. 然后在下面地址 https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey 注册一个 license key 以使用 google 提供的搜索服务。
1. 新建一个 Windows From 项目命名为 Google Search 。
2. 添加 Web 引用,以便使用 google web services 。具体做法是将下载的 the Google Web APIs Developer's Kit 中的 GoogleSearch.wsdl 文件放入本地的服务器上,然后在 VS 中右键点击资源管理器 中的引用 ,再选择添加 Web 引用 ,然后输入 GoogleSearch.wsdl 文件在本机服务器上的地址 http://localhost/GoogleSearch.wsdl ,更改 Web 引用名为 GoogleSearch 后,点击添加引用 后返回。
3. 进行界面设计,添加 3 个控件, TextBox( 用于输入关键字 ) , Button (用于提交信息), RichTextBox( 用于显示搜索结果信息 ) 。如图:
4. 编写事件处理。
1) 添加命名空间。 using Google_Search.googlesearch;
2) 双击 Button 控件,在 Button 的事件处理中添加下面的代码:
1 try 2 3
3) 按ctrl+F5就能测试应用程序了。
l GoogleSearchServices 类 doGoogleSearch 方法
需要进行搜索先建立一个 GoogleSearchServices 类的对象,然后调用doGoogleSearch 方法取得信息。下面是google 提供的对该方法的描述。
public doGoogleSearch ( string key , string q , int start , int maxResults , bool filter , string restrict , bool safeSearch , string lr , string ie , string oe )
下面对各个参数进行介绍:
Key : 这是由 google 提供的一个认证用的 ID ,可以在 http://www.google.com/apis/ 申请。由于目前是测试阶段,每个 ID 一天只提供 1000 次的搜索服务请求。
q: 这个就是要搜索的关键字。 string 类型
start: 结果开始的索引数,从 0 开始。 int 类型
maxresults: 返回的结果数的最大值,最大为 10 。 int 类型
filter: 用于标识是否对搜索的结果进行过滤,所进行的过滤是对于同一主机上的内容只返回 1—2 个结果。 bool 类型
restrict: 用于限定搜索的国家或地区,如果为空,表示不限制。 string 类型。
下表将列出所有能选的代号。
国家
代号
国家
代号
国家
代号
国家
代号
Andorra
countryAD
Estonia
countryEE
Kazakhstan
countryKZ
Qatar
countryQA
United Arab Emirates
countryAE
Egypt
countryEG
Lao People's Democratic Republic
countryLA
Reunion
countryRE
Afghanistan
countryAF
Western Sahara
countryEH
Lebanon
countryLB
Romania
countryRO
Antigua and Barbuda
countryAG
Eritrea
countryER
Saint Lucia
countryLC
Russian Federation
countryRU
Anguilla
countryAI
Spain
countryES
Liechtenstein
countryLI
Rwanda
countryRW
Albania
countryAL
Ethiopia
countryET
Sri Lanka
countryLK
Saudi Arabia
countrySA
Armenia
countryAM
European Union
countryEU
Liberia
countryLR
Solomon Islands
countrySB
Netherlands Antilles
countryAN
Finland
countryFI
Lesotho
countryLS
Seychelles
countrySC
Angola
countryAO
Fiji
countryFJ
Lithuania
countryLT
Sudan
countrySD
Antarctica
countryAQ
Falkland Islands (Malvinas)
countryFK
Luxembourg
countryLU
Sweden
countrySE
Argentina
countryAR
Micronesia , Federated States of
countryFM
Latvia
countryLV
Singapore
countrySG
American Samoa
countryAS
Faroe Islands
countryFO
Libyan Arab Jamahiriya
countryLY
St. Helena
countrySH
Austria
countryAT
France
countryFR
Morocco
countryMA
Slovenia
countrySI
Australia
countryAU
France, Metropolitan
countryFX
Monaco
countryMC
Svalbard and Jan Mayen Islands
countrySJ
Aruba
countryAW
Gabon
countryGA
Moldova
countryMD
Slovakia (Slovak Republic )
countrySK
Azerbaijan
countryAZ
United Kingdom
countryUK
Madagascar
countryMG
Sierra Leone
countrySL
Bosnia and Herzegowina
countryBA
Grenada
countryGD
Marshall Islands
countryMH
San Marino
countrySM
Barbados
countryBB
Georgia
countryGE
Macedonia , The Former Yugoslav Republic of
countryMK
Senegal
countrySN
Bangladesh
countryBD
French Quiana
countryGF
Mali
countryML
Somalia
countrySO
Belgium
countryBE
Ghana
countryGH
Myanmar
countryMM
Suriname
countrySR
Burkina Faso
countryBF
Gibraltar
countryGI
Mongolia
countryMN
Sao Tome and Principe
countryST
Bulgaria
countryBG
Greenland
countryGL
Macau
countryMO
El Salvador
countrySV
Bahrain
countryBH
Gambia
countryGM
Northern Mariana Islands
countryMP
Syria
countrySY
Burundi
countryBI
Guinea
countryGN
Martinique
countryMQ
Swaziland
countrySZ
Benin
countryBJ
Guadeloupe
countryGP
Mauritania
countryMR
Turks and Caicos Islands
countryTC
Bermuda
countryBM
Equatorial Guinea
countryGQ
Montserrat
countryMS
Chad
countryTD
Brunei Darussalam
countryBN
Greece
countryGR
Malta
countryMT
French Southern Territories
countryTF
Bolivia
countryBO
South Georgia and the South Sandwich Islands
countryGS
Mauritius
countryMU
Togo
countryTG
Brazil
countryBR
Guatemala
countryGT
Maldives
countryMV
Thailand
countryTH
Bahamas
countryBS
Guam
countryGU
Malawi
countryMW
Tajikistan
countryTJ
Bhutan
countryBT
Guinea-Bissau
countryGW
Mexico
countryMX
Tokelau
countryTK
Bouvet Island
countryBV
Guyana
countryGY
Malaysia
countryMY
Turkmenistan
countryTM
Botswana
countryBW
Hong Kong
countryHK
Mozambique
countryMZ
Tunisia
countryTN
Belarus
countryBY
Heard and Mc Donald Islands
countryHM
Namibia
countryNA
Tonga
countryTO
Belize
countryBZ
Honduras
countryHN
New Caledonia
countryNC
East Timor
countryTP
Canada
countryCA
Croatia (local name: Hrvatska)
countryHR
Niger
countryNE
Turkey
countryTR
Cocos (Keeling) Islands
countryCC
Haiti
countryHT
Norfolk Island
countryNF
Trinidad and Tobago
countryTT
Congo , The Democratic Republic of the
countryCD
Hungary
countryHU
Nigeria
countryNG
Tuvalu
countryTV
Central African Republic
countryCF
Indonesia
countryID
Nicaragua
countryNI
Taiwan
countryTW
Congo
countryCG
Ireland
countryIE
Netherlands
countryNL
Tanzania
countryTZ
Switzerland
countryCH
Israel
countryIL
Norway
countryNO
Ukraine
countryUA
Cote D'ivoire
countryCI
India
countryIN
Nepal
countryNP
Uganda
countryUG
Cook Islands
countryCK
British Indian Ocean Territory
countryIO
Nauru
countryNR
United States Minor Outlying Islands
countryUM
Chile
countryCL
Iraq
countryIQ
Niue
countryNU
United States
countryUS
Cameroon
countryCM
Iran (Islamic Republic of)
countryIR
New Zealand
countryNZ
Uruguay
countryUY
China
countryCN
Iceland
countryIS
Oman
countryOM
Uzbekistan
countryUZ
Colombia
countryCO
Italy
countryIT
Panama
countryPA
Holy See (Vatican City State )
countryVA
Costa Rica
countryCR
Jamaica
countryJM
Peru
countryPE
Saint Vincent and the Grenadines
countryVC
Cuba
countryCU
Jordan
countryJO
French Polynesia
countryPF
Venezuela
countryVE
Cape Verde
countryCV
Japan
countryJP
Papua New Guinea
countryPG
Virgin Islands (British)
countryVG
Christmas Island
countryCX
Kenya
countryKE
Philippines
countryPH
Virgin Islands (U.S. )
countryVI
Cyprus
countryCY
Kyrgyzstan
countryKG
Pakistan
countryPK
Vietnam
countryVN
Czech Republic
countryCZ
Cambodia
countryKH
Poland
countryPL
Vanuatu
countryVU
Germany
countryDE
Kiribati
countryKI
St. Pierre and Miquelon
countryPM
Wallis and Futuna Islands
countryWF
Djibouti
countryDJ
Comoros
countryKM
Pitcairn
countryPN
Samoa
countryWS
Denmark
countryDK
Saint Kitts and Nevis
countryKN
Puerto Rico
countryPR
Yemen
countryYE
Dominica
countryDM
Korea , Democratic People's Republic of
countryKP
Palestine
countryPS
Mayotte
countryYT
Dominican Republic
countryDO
Korea , Republic of
countryKR
Portugal
countryPT
Yugoslavia
countryYU
Algeria
countryDZ
Kuwait
countryKW
Palau
countryPW
South Africa
countryZA
Ecuador
countryEC
Cayman Islands
countryKY
Paraguay
countryPY
Zambia
countryZM
Zaire
countryZR
safeSearch: 使用一个 bool 值表示是否过滤垃圾信息。
lr: 对语言的限制, string 类型。 可选代号如下表:
语言
代号
语言
代号
Arabic
lang_ar
Icelandic
lang_is
Chinese (S)
lang_zh-CN
Italian
lang_it
Chinese (T)
lang_zh-TW
Japanese
lang_ja
Czech
lang_cs
Korean
lang_ko
Danish
lang_da
请发表评论