Google Web API 的 ASP.NET Web 客户端






4.44/5 (17投票s)
2003 年 1 月 26 日
2分钟阅读

251944

2870
探索谷歌网络 API。
引言
最近我有机会探索 Google Web API,可以从 Google 网站 下载。所以我决定编写一个 Web 客户端,这非常简单而且有趣!本文重点介绍开发 Web 客户端,你可以通过它从你自己的网站上搜索项目。
安装和设置
首先你需要下载 google web api!然后按照以下说明操作。
- 在 "wwwroot" 文件夹中创建一个名为 "Net" 的文件夹,并将该文件夹设置为 Web 应用程序。
- 在 "Net" 文件夹中创建一个 "bin" 子文件夹。
- 使用 WSDL 工具创建
GoogleProxy.cs
,并使用csc
命令将GoogleProxy.cs
编译成 .net 程序集,以生成代理。>wsdl /l:cs /o:GoogleProxy.cs "https:///Net/GoogleSearch.wsdl">http://localhost/Net/GoogleSearch.wsdl
/n:GoogleWebService
这将生成GoogleProxy.cs
。“GoogleSearch.wsdl
”可以在你下载的 API 中找到。>csc /out:GoogleProxy.dll /t:library /r:system.dll, system.web.dll,
system.xml.dll, system.web.services.dll GoogleProxy.cs
这将生成 .net 程序集 GoogleProxy.dll。将 dll 文件复制到 "bin" 文件夹。你的 ASP.NET 页面最终将调用此 dll 暴露的 Web 可调用方法和属性。
- 编写
GoogleClient.aspx
文件以创建 UI 并使用 google web api 暴露的服务,在本例中为 GoogleProxy.dll。 - 浏览
http://localhost/Net/GoogleClient.aspx
。就是这样!
为什么使用代理?
代理驻留在消费者机器上,充当消费者和 Web 服务之间的中介。当我们构建代理时,我们使用 WSDL 文件创建一个映射,告诉消费者有哪些方法可用以及如何调用它们。然后,消费者调用代理中映射的 Web 方法,该方法反过来通过互联网向实际的 Web 服务发出调用。代理处理所有与网络相关的操作和数据发送,以及管理底层的 WSDL,因此消费者不必处理。当我们引用消费者应用程序中的 Web 服务时,它看起来就像它是消费者应用程序的一部分。
使用代码
代码非常简单明了。
<%@ Page Language="C#" %> <@ import Namespace="GoogleWebService" > //Remember,GoogleWebService is the namespace you named while creating the proxy! <script runat="server"> string key="licence key you got from google"; /* I have declared the key string variable as global variable since the key variable is to be passed every time you call the methods. */ void Page_Load() { lblSpellSug.Text=""; //Label to display the Spelling Suggestion lblResultCount.Text=""; //Label to display the Estimated total result count lblSearchTime.Text=""; //Label to display the server time to return the search results, //measured in seconds. } void btnSearch_Click(Object sender, EventArgs e) { //creating the instance of the GoogleSearch class to invoke required methods GoogleSearchService obj=new GoogleSearchService(); //spelling checking and suggesting if entered wrong string suggestion=obj.doSpellingSuggestion(key,Request.Form["txtPhrase"]); if (suggestion!=null) { lblSpellSug.Text="Suggestion: "+ suggestion; } //searching the phrase..... //Regarding the parameters refer to the Google API GoogleSearchResult res=obj.doGoogleSearch(key, Request.Form["txtPhrase"], 0, 10, false,"",false,"","",""); lblResultCount.Text="Est. Total Result Count: " + Convert.ToString(res.estimatedTotalResultsCount); //to display the total estimated result count lblSearchTime.Text= "Search Time: " + Convert.ToString(res.searchTime) + "sec";//search Time //displaying the results Returned by the Search in tabular using the Table control. ResultElement[] result=res.resultElements; foreach(ResultElement r in result) { ResultTable.CellSpacing=1; ResultTable.CellPadding=2; //formatting the Server control Table TableRowCollection trCol=ResultTable.Rows; //ResultTable is the instance created for Table class //creating new Table Row and adding to the TableRowCollection TableRow tr=new TableRow(); trCol.Add(tr); TableCellCollection tcCol=tr.Cells; //creating new Table Cell, assigning the title and the summary //of the search result. TableCell tc=new TableCell(); tc.Text="<a href="+ r.URL +">"+ r.title + "</a>" + "<BR>" + r.summary; tcCol.Add(tc); } } </script>
结论
让我们分享解决方案。