设为首页收藏本站language 语言切换
查看: 1090|回复: 0
收起左侧

使用VB.NET实现 Google Web Service

[复制链接]
发表于 2010-2-25 10:26:40 | 显示全部楼层 |阅读模式
<p >web service 是当今因特网世界中最重要的开发技术之一,我们可通过使用XML (可扩展标记语言)、 SOAP (简单对象访问协议)、WSDL (Web Services 描述语言)和UDDI (统一描述、发现和集成协议)以标准方式将web service技术用于业务应用和客户端之间的连接。 <p >XML可用于构造数据、SOAP 可用于数据传输、WSDL 可用于描述服务而UDDI 可用于获取可用服务的列表。Web service 使得应用程序无须考虑硬件系统、操作系统和编程语言就可以进行相互通信。<p >web service与以前的模型不同之处就在于它本身并不提供用户接口,相反web service公开揭示了可编程的业务逻辑。因此,用户可根据需要将自己的接口添加到应用程序中。<p >本文中,我们将会学习如何使用 Microsoft Visual Basic.Net来实现Google web service。<p ><center><font color="#000099"><strong>Google Web Service </strong></font></center><p >Google是一个很重要的web站点,它向公众提供web服务,允许应用程序使用如搜索和拼写检查之类的功能。现在,我们来看一看如何通过Visual Basic.NET在应用程序中使用此服务。<p >在访问Google web service之前,我们需要创建一个Google帐户并获得一个许可密钥,只有这样,我们才能在一天中进行1000 个左右的自动查询。<p >创建Google 帐户时请访问http://www.Google.co.nz/apis/。一旦输入了电子邮件地址和口令,Google 就会通过email 将您的许可密钥发送到您的信箱中。我们会在本文的示例中使用许可密钥。<p ><b>从这里开始</b><p >现在我们已经获得了所需的许可密钥,接下来我们将在Visual Basic.NET中创建一个应用程序,以便通过使用Google的web service API(应用程序编程接口)来创建自定义的搜索和拼写检查器。 <p >请打开 Visual Studio .NET ,然后创建一个新的Windows 应用程序工程。将此工程命名为googleapi 并单击确定: <p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_ylgaps43678.gif"></center><p ><b>添加指向Google Web Service的Web引用</b><p >下一步,我们需要添加指向Google Web Service的Web引用(这与添加指向 COM/ActiveX 对象的引用非常相似,但是添加Web引用后,我们就有权访问Google 服务器上的XML web service)。 <p >请打开您的solution explorer,右键单击references并单击添加web reference,或者您可以选择工程菜单然后单击add web reference。<p >在地址栏中,请键入http://api.Google.com/GoogleSearch.wsdl (注意:请确保你所键入的内容正确无误,即与所显示的完全一样,尤其要注意该 URL是区分大小写的):<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_tdp2r743679.gif"></center><p >在输入URL地址并按下回车键之后,Google web service就会导入,您看到的屏幕应该与上面示例中所显示的窗口类似。最后,单击添加引用按钮将此web 引用添加到我们工程中。<p ><b>执行Google Web Service </b><p >请在解决方案浏览器窗口中单击web 引用,这样就可以查看我们在此之前已添加的Google web 引用 。我们将其重新命名为Google,具体方法是右键单击此引用并单击重新命名:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_rejygu43681.gif"></center><p >创建用户接口,如下图所示。添加下列控件:<p >a) 用于搜索: <p >txtSearch - 文本框 <p >lbl_TotalFound - 标签 <p >btn_Search - 按钮 <p >b) 用于拼写检查: <p >txt_CheckSpelling - 文本框 <p >lbl_CorrectSpelling - 标签 <p >btn_CheckSpelling 按钮 <p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_d0u2pv43680.gif"></center><p >请将下列代码输入到Google 搜索按钮(btn_Search)的单击事件中: <p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>rivate 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 = &quot;tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM&quot; ' Execute Google search on the text enter and license key MyResult = MyService.doGoogleSearch(MyLicenseKey, _ txtSearch.Text, 0, 1, False, &quot;&quot;, False, &quot;&quot;, &quot;&quot;, &quot;&quot;) ' output the total Results found lbl_TotalFound.Text = &quot;Total Found : &quot; &amp; _ CStr(MyResult.estimatedTotalResultsCount) End Sub</ccid_code></pre></td></tr></table></ccid_nobr><p >请将下列代码输入到拼写检查按钮(btn_CheckSpelling)的单击事件中: <p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>rivate 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 = &quot;tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM&quot; ' Execute Google search on the text enter and license key MyResult = MyService.doSpellingSuggestion(MyLicenseKey, _ txt_CheckSpelling.Text) ' output the Results lbl_CorrectSpelling.Text = &quot;Did you Mean : &quot; &amp; MyResult End Sub</ccid_code></pre></td></tr></table></ccid_nobr><p >现在我们已完成了应用程序的编码工作,接下来就可以运行应用程序并在搜索框中键入文本,然后单击google 搜索按钮查看所找到的结果的数目。我们还可以对Google拼写检查进行测试和验证。<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_ne5cfa43682.gif"></center><p >我们的web service 实现了预期的目标,可正常运转。而我们的实现却仅仅寥寥几行代码而已!<p ><center><font color="#000099"><strong>结论 </strong></font></center><p >本文详细描述了如何将web service 集成到应用程序中。您可以对此访问进行如下处理: <p >·发布定期订阅的搜索请求以监控web 有关某一主体的新信息。<p >·进行市场调查,方法是分析不同主题可用信息量的差别。<p >·通过非HTML 接口进行搜索,如命令行、论文或者可视化应用程序。<p >·开展创新活动来充分使用 web 上的信息。 <p >·将 Google 拼写检查添加到应用程序。<p >Google web service 支持的搜索语法与 Google.com web 站点所支持的搜索语法相同。同时它为注册使用 Google web service 的开发用户每天提供最多1,000 次查询(这个数目对于小型/中型应用程序来说已经足够使用了)<p >(责任编辑 <ccid_nobr>Sunny</ccid_nobr>)                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 15:49 , Processed in 0.062525 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表