从Win Forms应用程序中使用Web服务






3.78/5 (40投票s)
2002 年 8 月 15 日
3分钟阅读

494983

11443
如何使用 CodeProject Web 服务,从 C# 应用程序访问和使用 Web 服务。
引言
v. con·sumed, con·sum·ing, con·sumes
v. tr.
...
2.
a. To expend; use up.
b. To purchase (goods or services) for direct use or ownership.
...
5. To absorb; engross.
使用 XML Web 服务很像去餐厅点菜,然后吃东西或者“消耗”它。 对于这篇文章,我坐在 CodeProject Cafe 的酒吧里,拿到了一份菜单。 我告诉 Chris(酒保)我想要一杯啤酒和最新的文章列表,几秒钟后他就把它们放在我面前。 然后我消耗了他给我的东西,快乐地离开了。
但是使用 Chris 为我们提供的这些服务需要什么呢? 好吧,这就是本文的内容,请继续阅读。
创建一个 Win Forms 应用程序
在本文中,我将使用 C# Windows 应用程序作为我的 Web 服务使用者,但是你可以使用 VB.NET、Managed C++ 或任何其他 .NET 语言,从 ASP.NET 网页或任何其他 .NET 项目中使用 Web 服务。 这里介绍的演示项目将从 CodeProject 检索最新的文章。
创建应用程序不在本文的范围内,所以我假设你已经知道如何创建。 如果没有,这里有一些参考资料
添加 Web 引用
要使用/消耗 Web 服务,你必须首先告诉你的应用程序在哪里找到它。
在解决方案资源管理器中,右键单击你的项目,然后单击“添加 Web 引用...”,或者从“项目”菜单中,单击“添加 Web 引用...”。 在地址栏中,键入 Web 服务的 URL。 对于此示例,使用 https://codeproject.org.cn/webservices/latest.asmx。
你应该在窗口的浏览器部分看到一个LatestBrief 网页。
单击“添加引用”按钮以完成将 Web 引用添加到项目。
使用 Web 引用
添加 Web 引用时,VS.NET 使用 WSDL 文件创建一个Reference.cs,其中包含 WSDL 中定义的 C# 命名空间和类(CP Cafe 菜单)。
你将在应用程序中使用命名空间、类和方法来消耗 CodeProject 的“啤酒和最新文章”。
消费 CP 的 Web 服务
由于已添加 Web 引用,我们可以开始编写一些代码。 遗憾的是,你实际上无法从 CP 订购啤酒(还不能 ;)),但是让我们继续看看我们如何从 CP Web 服务获取最新文章。
...
using CPConsumer.com.codeproject.www;
namespace CPConsumer
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class CPConsumerForm : System.Windows.Forms.Form
{
...
private LatestBrief cpLatestBrief = null;
public CPConsumerForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// instantiate the LatestBrief class
cpLatestBrief = new LatestBrief();
}
添加 using CPConsumer.com.codeproject.www;
,Web 引用的命名空间,为了方便起见。
声明一个名为 cpLatestBrief
的类型为 LatestBrief
的私有成员变量,并将其设置为 null
。 这是通过 WSDL 生成的我们将要使用的类。 在构造函数中,我们使用 new
实例化它。
private int GetNumArticles()
{
// get the maximum number of article briefs we can get from CP
if (cpLatestBrief != null)
return cpLatestBrief.GetMaxArticleListLength();
else
return 0;
}
接下来,我们想找到可以从 Web 服务获取的最大文章数量。 我们通过使用 WSDL 和 LatestBrief
类中定义的 GetMaxArticleListLength
函数来实现。
我们只是消耗了 CP Web 服务的一部分!
// article brief array
ArticleBrief [] cpArticles = null;
// max article briefs
int iNumArticles = GetNumArticles();
// clear the list
ArticleList.Items.Clear();
if (iNumArticles > 0 && cpLatestBrief != null)
{
// get the article briefs from the web service
cpArticles = cpLatestBrief.GetLatestArticleBrief(iNumArticles);
if (cpArticles != null)
{
// add them all to the list view
for (int i=0; i<iNumArticles; i++)
{
ListViewItem lvi = new
ListViewItem(cpArticles[i].Updated.ToString());
lvi.SubItems.Add(cpArticles[i].Title);
lvi.SubItems.Add(cpArticles[i].Author);
lvi.SubItems.Add(cpArticles[i].Description);
lvi.SubItems.Add(cpArticles[i].URL);
ArticleList.Items.Add(lvi);
}
...
}
}
在演示项目中,我使用最新的 CP 文章填充列表视图。 要获取这些文章,请创建一个 ArticleBrief
数组,该数组将由 GetLatestArticleBrief
方法填充。
在数组被填充后,ListView
将填充文章的详细信息。 在 ListView
中选择一篇文章将为你提供一个链接和 ListView
下方标签中的描述。
结论
我永远不会想到 Web 服务会如此易于使用。 感谢 Chris M. 让我们能够使用 CodeProject Web 服务。