[译]完全可定制的Google API Web客户端
By robot-v1.0
本文链接 https://www.kyfws.com/applications/a-completely-customizable-google-api-web-client-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2722 个词 阅读量 0[译]完全可定制的Google API Web客户端
原文地址:https://www.codeproject.com/Articles/4555/A-completely-customizable-Google-API-Web-Client
原文作者:Vasudevanms
译文由本站 robot-v1.0 翻译
前言
A web based Google Web services client using ASP.NET.
使用ASP.NET的基于Web的Google Web服务客户端.
介绍(Introduction)
这是Google Web Services的基于Web的客户端的演示.这是我第一次尝试ASP.NET技术.(This is a demonstration of a web based client for Google Web Services. This is my first attempt at ASP.NET technology.)
我试图使其完全可自定义,并尽可能做到开放式.(I have tried to make it fully customizable and open ended as far as possible.)
关键的自定义功能是:(Key customization features are:)
- 搜索结果显示为(The search result display is)**受控(controlled)**通过XSL文件((by an XSL file ()search.xsl(search.xsl)).改变外观,并以所需的方式自定义结果集,这很有趣.(). Change the look and feel and have fun customizing the result set the way you want it.)
- 该应用程序从(The application reads from a)cfg.xml(cfg.xml)启动文件.它包含所有搜索查询参数以及一些特定于应用程序的设置.例如,访问(file on start up. It contains all the search query parameters and also some application specific settings. For example, the key required to access the)谷歌搜索数据库(Google search database).您可以在附加的zip文件中找到此文件.(. You can find this file in the attached zip fie.) 该应用程序寻找(The app looks for)*cfg.xml(cfg.xml)*在当前工作目录中(in the current working directory which would be)*c:\ winnt \ system32(c:\winnt\system32)*默认情况下.将此文件复制到该位置.(by default. Copy this file to that location.)
这样做的优点是可以随需求的变化而更改搜索查询词,并且对于结果显示也同样有用.(Advantage of having this is search query terms can be changed as and when requirements change, and same holds good for the results display.)
由于Google设置了该限制,因此该应用程序允许用户浏览前1000个结果.(This application allows a user to go through the first 1000 results since Google places that restriction.)
您需要什么来运行演示项目?(What do you need to run the demo project?)
- Windows 2000/.NET Framework 1.0版上的IIS 5.0.(IIS 5.0 on Windows 2000/.NET Framework version 1.0.)
- **需要:(Required:)**向Google注册并获取您的个人许可证密钥.(Register with Google and get your personal license key.)这是必须的(This is a must).(.)
- **需要:(Required:)**打开(Open the)cfg.xml(cfg.xml)包括在源中并填写以下值:(included in the source and fill in the values for)
<key/>
,(,)<ResultsPath/>
和(and)<XslPath/>
.稍后解释这些键…(. Explanation of these keys in a moment…)
该应用程序做什么(What the Application does)
第1步:(Step 1:)
该应用程序将SOAP消息发送到Google服务器,该服务器随后返回Google专有数据结构.然后,应用程序解析此数据结构并创建一个XML文件. (让我们称之为(The application sends a SOAP message to the Google Server which in turn returns a Google proprietary data structure. The application then parses this data structure and creates an XML file. (Let’s call it)results.xml(results.xml).(.)注意:您可以选择任何名称,只要它在(Note: It could be any name you choose as long as it’s in the)cfg.xml(cfg.xml).)这只是其中的一小部分:(.) Here is a small portion of it:)
<SearchResults attributes1=value1... >
<ResultSet>
<Item>
<summary />
<url>http://www.iqsoft.com/</url>
<snippet>PRODUCTIONS </snippet>
<title>Welcome to SAWStudio.com!</title>
<cachedsize>9k</cachedsize>
<relatedInformationPresent>True</relatedInformationPresent>
<hostName />
<directorytitle />
</Item>
</ResultSet>
</SearchResults>
<SearchResults>
第2步:(Step 2:)
然后,应用程序使用自定义样式表转换此XML文件((The application then transforms this XML file using the custom stylesheet ()search.xsl(search.xsl))并将转换结果写入客户端浏览器.() and writes the results of the transformation to the client browser.)
好,那就是(OK, so that’s what the) <ResultsPath/>
和(and) <XslPath/>
键的意思.(keys mean.)
<ResultsPath/>
是您希望应用程序将XML转储到的文件的标准路径.(is the fully qualified path to the file where you want the app to dump the XML.)<XslPath />
是样式表((is the fully qualified path to the file where the stylesheet ()search.xsl(search.xsl)) 被储存了.() is stored.)
源代码的关键区域:(Key areas of the source code:)
WebForm1.aspx.cs中的搜索类(Search class in WebForm1.aspx.cs)
的(The) Search
在以下代码中定义的类(class defined in the code-behind of)WebForm1.aspx(WebForm1.aspx),是整个项目中唯一使用的网络表单,具有核心功能(, which is the only webform used in the entire project, has the core function) dumpResultsasXml()
确实与命名一样.(which does exactly as named.) System.XmlTextWriter
做写作.(does the writing.)
的(The) Transform()
函数将转换为HTML.我用过(function does the transformation to HTML. I have used a) MemoryStream
对象以读取转换结果,然后读取(object to read the transformation results, and subsequently, a) StreamReader
读取该内存并将其转换为字符串数据类型.(to read that memory and convert it to string data type.)
的(The) WebForm1.getdata
是动作实际发生的地方.可以从内部调用(is where the action actually happens. This can be called from within the)*WebForm1.aspx(WebForm1.aspx)*像这样:(like this:)
<% Response.Write(getdata()) %>
兴趣点:(Points of interest:)
Google Search Service对象只有一个实例,该实例在中初始化(There is only a single instance of the Google Search Service object which is initialized in) Application_Start()
.(.)
尽管"下一个"和"上一个"链接可能是由(Although the “Next” and “Previous” links could have been generated by the)*search.xsl(search.xsl)*文件,除了递增XSL之外,我们无法控制XSL中的起始索引值(file, there is no way for us to control the starting index value in the XSL other than incrementing it by) x
像这样:(like this:)
<xsl:variable name="startindex" select="@currentIndex+10"/>
… 等等.(… and so on.)
但是XSL会很高兴地不知道我们在哪个页面上.以下代码显示了其实现方式:(But the XSL would be blissfully unaware of what page we are on. The following code shows how it’s been implemented:)
if( !Page.IsPostBack)
{
start = Request.Params["start"];
results = getdata(query,Convert.ToInt32(start));
}
else
results = getdata(query,0); //In this case start would always be 0.
使用querystring参数进行跟踪(Tracking is done using the querystring parameter) start
到哪个(to which) x
添加或减去,以生成"下一个"和"上一个"链接的URL.在这种情况下,(is added or subtracted, to generate the URL for the “Next " and “Previous” links. In this case,) x
=10.最好将此作为设置中的一项(=10. It’s a good idea to have this as one of the settings in the)cfg.xml(cfg.xml),因此会更通用.好吧,下一个版本也许是:)(, so it would be more generic. Well, next release maybe :))
这是一个如何(Here is an example of how the)下一个(Next)链接已生成,其中start是querystring参数的值(link is generated, where start is the value of the querystring parameter) start
.(.) nTotalCount
是结果总数.(is the total number of results.)
if((nTotalCount - nNext) > 10)
{
Response.Write("<br></br><a href=\"webform1.aspx?type=Next&start=");
Response.Write(nNext);
Response.Write("&query=");
Response.Write(TextBox1.Text);
Response.Write("\"><b>Next</b></a><br></br>");
}
好吧,我希望我已经涵盖了关键领域.像其他任何程序一样,我敢肯定我可能会犯过错误和错误.期待您的反馈,因此我可能会合并增强功能和错误修复程序(如果有).(Well, I hope I have covered the key areas. Like any other program, I am certain there would be bugs and mistakes I may have made. Looking forward to your feedback so I may incorporate enhancements and bug fixes if any.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.0 VC7.1 C++ VC6 Win2K WinXP .NET1.0 Visual-Studio ASP.NET Dev 新闻 翻译