使用 SOAP Web 服务 - 亚马逊、谷歌等






4.77/5 (15投票s)
本文介绍了如何构建和使用静态代理类来访问 Web 服务。
引言
Web 服务基于 SOAP,这是一个基于 XML 的协议,它建立了 Web 服务使用者和提供者之间消息的形式。 传统上,使用应用程序的开发人员将手动构建 SOAP 消息并将其发送到服务器,然后服务器将以 SOAP 消息的格式返回其答案。 理解 SOAP 的要求阻碍了 Web 服务的采用。 为了解决这个问题,.NET 框架可以封装大量的复杂性——以至于开发人员根本不必了解消息的底层结构。 事实上,Visual Studio 使 Web 服务的使用过程更加容易,但并非每个人都可以访问它。 因此,我决定编写一个示例,在这个示例中,我们只使用了作为免费 SDK 一部分的命令行工具,另外的好处是,通过这种方式,您可以了解更多关于幕后发生的事情!.NET 提供的简化是通过以下方式实现的。 给定 Web 服务的接口在 Web 服务描述语言 (WSDL) 中定义为一个 XML 文件。 WSDL 列出了方法、返回类型和方法参数。 可以以编程方式分析此接口,并在您选择的语言中静态构建一个存根类,该类封装了构建和传输底层 SOAP 消息的所有复杂性。 微软提供了一个工具 wsdl.exe,它接受对 WSDL 文件的引用,并为您生成这些静态存根。
下面的例子应该会更清楚。
示例
我在 Xmethods.NET 上找到一个 Web 服务,该服务每次调用时都会返回一个幸运饼干风格的报价。 看看这里的 WSDL here。 重要的是下面显示的,它描述了getFortune
方法。 <operation name="getFortune">
<input name="getFortune0Request" message="tns:getFortune0Request" />
<output name="getFortune0Response" message="tns:getFortune0Response" />
</operation>
步骤 1:构建代理类
执行以下命令...C:>wsdl.exe http://adrianr.dyndns.org/Fortune/Fortune.wsdl... 结果是 C# 文件 Fortune.cs,其中包含一个名为 Fortune 的类。 检查这个类的方法如何对应于 WSDL 文件中详细描述的方法,尤其是以下方法
public string getFortune();另请注意,生成的 Fortune 类继承了
System.Web.Services.Protocols.SoapHttpClientProtocol
。 这个类封装了实际的 SOAP 消息生成和套接字传输。(在本例中,C# 被用作构建代理类的首选语言。 命令行标志
/language
可用于使用 VB、JS 等构建存根。)
步骤 2:编译代理类
接下来,我们必须编译自动生成的文件。 该文件不包含入口点,因此必须作为库构建。C:\>csc /t:library c:\Fortune.cs结果是一个新文件 Fortune.dll。
步骤 3:测试服务
下一阶段是编写一个简单的测试程序。using System; using System.IO; class WSConsumer { public static void Main() { Fortune f = new Fortune(); Console.WriteLine( f.getFortune() ); } };编译此类时,我们必须链接到之前创建的 Fortune.dll。
C:\>csc WSConsumer.cs /r:Fortune.dll然后就可以运行生成的 WSConsumer.exe 找到你的幸运签 :) 了
C:\>wsconsumer "Security is when everything is settled. When nothing can happen to you. Security is the denial of life." -- Germaine Greer (b. 1939), Australian feminist, writer, "The Female Eunuch" C:\>wsconsumer "The mode by which the inevitable comes to pass is effort." -- Sri da Avabhas (Adi Da Samraj)
使用 FortuneCookie、亚马逊、谷歌和 Code Project Web 服务
CodeProject 的人已经设计了访问 谷歌 和 Code Project Web 服务的工具。 我的例子没有任何 GUI 或错误检查,所以我希望我的尝试可能更清楚。 如前所述,我也认为 Visual Studio.NET 隐藏了开发人员的存根生成。 无论如何,亚马逊和前面提到的 Fortune Cookie 服务的添加是新的。这四个 DLL 都是使用 WSDL.exe 创建和构建的。
12/13/2003 07:42 PM 4,608 Fortune.dll
12/13/2003 09:31 PM 13,824 LatestBrief.dll
12/13/2003 09:46 PM 6,144 GoogleSearchService.dll
12/13/2003 10:12 PM 40,960 AmazonSearchService.dll
然后,使用这些服务的源代码 TestWebServices.cs 就一目了然了。 唯一的细微之处在于,大多数 Web 服务将采用或返回需要进一步处理以准备或从中获取信息。 例如,亚马逊添加了 KeywordRequest
对象的概念...
KeywordRequest req = new KeywordRequest();
req.keyword = "Web Services";
req.page = "1";
req.type = "lite";
req.devtag = "D4HGDDVSYO30N";
req.mode = "books";
ProductInfo products = amazon.KeywordSearchRequest( req );
... 并且需要从谷歌中提取搜索结果
GoogleSearchResult result = google.doGoogleSearch( key, // License key
"codeproject", // Search term
0, // Start at
10, // Max results
true, // Filter
"", // Restrict
true, // SafeSearch
"", "", "" ); // ?
// ResultElement describes one result of the search
foreach( ResultElement res in result.resultElements )
{
Console.WriteLine( " " + res.title + " (" + res.cachedSize + ")" );
}
同样,很容易从自动生成的存根中确定如何处理这些问题。编译测试程序涉及链接到每个 DLL。
C:\>csc TestWebServices.cs /r:GoogleSearchService.dll
/r:LatestBrief.dll /r:Fortune.dll /r:AmazonSearchService.dll
最后,运行 TestWebServices.exe 应该会向您呈现以下内容。 很容易看出如何以有趣的方式集成这些东西。
C:\>testwebservices
Usage: TestWebServices [GoogleKey] [AmazonKey]
C:\>testwebservices
A fortune cookie:
"Justice is always violent to the party offending, for every man
is innocent in his own eyes."
-- Daniel Defoe (1660-1731), English journalist,
novelist
Code Project Latest Articles:
Add GPIB Support to Your Desktop (Available, New)
SheetView Control (Available, New)
Customising Visual Studio's Code Generation Templates (Available, New)
Customising Visual Studio's Code Generation Templates (Available, New)
Getting the most out of IDispatch (Available, Updated)
For fun, lets query Google:
The Code Project - Free Source Code and Tutorials (47k)
The Code Project - Free Source Code and Tutorials (101k)
The Code Project - The Code Project Search Bar - Free Tools (59k)
The Code Project - Free Source Code and Tutorials (46k)
Freesticky - Code Project Syndication (47k)
codeproject.com (7k)
syz.de - Links (33k)
CodeProject (16k)
CodingCommunity - Linkliste : Linkinfo (10k)
CodeProject Object (Microsoft Access 2002 Visual Basic Language ... (12k)
Amazon web service:
eBay for Dummies (0764516426)
Developing Java Web Services: Architecting and
Developing Secure Web Services Using Java (0471236403)
.NET Web Services: Architecture and Implementation with .NET (0321113594)
Active Directory For Dummies (0764506595)
Executive's Guide to Web Services (0471266523)
Loosely Coupled: The Missing Pieces of Web Services (1881378241)
Absolute BSD: The Ultimate Guide to FreeBSD (1886411743)
欲了解更多信息
关于这里四个服务的信息
这些网站也很有用