使用VB.NET实现 Google Web Service

Google Web Service相关探讨
博客围绕Google Web Service展开,虽未给出具体内容,但可推测是对其进行相关探讨。涉及VB.NET、VB、.net等技术,与信息技术领域紧密相关。
web service 是当今因特网世界中最重要的开发技术之一,我们可通过使用XML (可扩展标记语言)、 SOAP (简单对象访问协议)、WSDL (Web Services 描述语言)和UDDI (统一描述、发现和集成协议)以标准方式将web service技术用于业务应用和客户端之间的连接。

XML可用于构造数据、SOAP 可用于数据传输、WSDL 可用于描述服务而UDDI 可用于获取可用服务的列表。Web service 使得应用程序无须考虑硬件系统、操作系统和编程语言就可以进行相互通信。

web service与以前的模型不同之处就在于它本身并不提供用户接口,相反web service公开揭示了可编程的业务逻辑。因此,用户可根据需要将自己的接口添加到应用程序中。

本文中,我们将会学习如何使用 Microsoft Visual Basic.Net来实现Google web service。

Google Web Service


Google是一个很重要的web站点,它向公众提供web服务,允许应用程序使用如搜索和拼写检查之类的功能。现在,我们来看一看如何通过Visual Basic.NET在应用程序中使用此服务。

在访问Google web service之前,我们需要创建一个Google帐户并获得一个许可密钥,只有这样,我们才能在一天中进行1000 个左右的自动查询。

创建Google 帐户时请访问 http://www.Google.co.nz/apis/。一旦输入了电子邮件地址和口令,Google 就会通过email 将您的许可密钥发送到您的信箱中。我们会在本文的示例中使用许可密钥。

从这里开始

现在我们已经获得了所需的许可密钥,接下来我们将在Visual Basic.NET中创建一个应用程序,以便通过使用Google的web service API(应用程序编程接口)来创建自定义的搜索和拼写检查器。

请打开 Visual Studio .NET ,然后创建一个新的Windows 应用程序工程。将此工程命名为googleapi 并单击确定:

43678.gif


添加指向Google Web Service的Web引用

下一步,我们需要添加指向Google Web Service的Web引用(这与添加指向 COM/ActiveX 对象的引用非常相似,但是添加Web引用后,我们就有权访问Google 服务器上的XML web service)。

请打开您的solution explorer,右键单击references并单击添加web reference,或者您可以选择工程菜单然后单击add web reference。

在地址栏中,请键入http://api.Google.com/GoogleSearch.wsdl (注意:请确保你所键入的内容正确无误,即与所显示的完全一样,尤其要注意该 URL是区分大小写的):

43679.gif


在输入URL地址并按下回车键之后,Google web service就会导入,您看到的屏幕应该与上面示例中所显示的窗口类似。最后,单击添加引用按钮将此web 引用添加到我们工程中。

执行Google Web Service

请在解决方案浏览器窗口中单击web 引用,这样就可以查看我们在此之前已添加的Google web 引用 。我们将其重新命名为Google,具体方法是右键单击此引用并单击重新命名:

43681.gif


创建用户接口,如下图所示。添加下列控件:

a) 用于搜索:

txtSearch - 文本框

lbl_TotalFound - 标签

btn_Search - 按钮

b) 用于拼写检查:

txt_CheckSpelling - 文本框

lbl_CorrectSpelling - 标签

btn_CheckSpelling 按钮

43680.gif


请将下列代码输入到Google 搜索按钮(btn_Search)的单击事件中:

<ccid_nobr><table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="550" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" bgcolor="#e6e6e6"><pre><ccid_code>Private Sub btn_Search_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btn_Search.Click Dim MyLicenseKey As String ' Variable to Store the License Key ' Declare variable for the Google search service Dim MyService As Google.GoogleSearchService = New _ Google.GoogleSearchService() ' Declare variable for the Google Search Result Dim MyResult As Google.GoogleSearchResult ' Please Type your license key here MyLicenseKey = "tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM" ' Execute Google search on the text enter and license key MyResult = MyService.doGoogleSearch(MyLicenseKey, _ txtSearch.Text, 0, 1, False, "", False, "", "", "") ' output the total Results found lbl_TotalFound.Text = "Total Found : " &amp; _ CStr(MyResult.estimatedTotalResultsCount) End Sub</ccid_code></pre></td></tr></tbody></table></ccid_nobr>

请将下列代码输入到拼写检查按钮(btn_CheckSpelling)的单击事件中:

<ccid_nobr><table cellspacing="0" bordercolordark="#ffffff" cellpadding="2" width="550" align="center" bordercolorlight="black" border="1"><tbody><tr><td class="code" bgcolor="#e6e6e6"><pre><ccid_code>Private Sub btn_CheckSpelling_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btn_CheckSpelling.Click Dim MyLicenseKey As String ' Variable to Store the License Key ' Declare variable for the Google search service Dim MyService As Google.GoogleSearchService = New _ Google.GoogleSearchService() ' Declare variable for the Google Search Result Dim MyResult As String ' Please Type your license key here MyLicenseKey = "tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM" ' Execute Google search on the text enter and license key MyResult = MyService.doSpellingSuggestion(MyLicenseKey, _ txt_CheckSpelling.Text) ' output the Results lbl_CorrectSpelling.Text = "Did you Mean : " &amp; MyResult End Sub</ccid_code></pre></td></tr></tbody></table></ccid_nobr>

现在我们已完成了应用程序的编码工作,接下来就可以运行应用程序并在搜索框中键入文本,然后单击google 搜索按钮查看所找到的结果的数目。我们还可以对Google拼写检查进行测试和验证。

43682.gif


我们的web service 实现了预期的目标,可正常运转。而我们的实现却仅仅寥寥几行代码而已!

结论


本文详细描述了如何将web service 集成到应用程序中。您可以对此访问进行如下处理:

·发布定期订阅的搜索请求以监控web 有关某一主体的新信息。

·进行市场调查,方法是分析不同主题可用信息量的差别。

·通过非HTML 接口进行搜索,如命令行、论文或者可视化应用程序。

·开展创新活动来充分使用 web 上的信息。

·将 Google 拼写检查添加到应用程序。

Google web service 支持的搜索语法与 Google.com web 站点所支持的搜索语法相同。同时它为注册使用 Google web service 的开发用户每天提供最多1,000 次查询(这个数目对于小型/中型应用程序来说已经足够使用了)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值