Consuming a Web Service from a Win Form Application
Consuming a Web Service from a Win Form Application
Saurabh Nandu
April 12 2001
Level: Beginner/Intermediate
Download Source Code
Introduction
In the previous .NET101 article Building a Stock-Quotes Web
Service, we saw how to build a Stock-Quotes Web Service and consume it from a
ASP.NET web page. In this article we will concentrate on consuming the
Stock-Quotes Web Service from a Win Form Application.
This article
assumes that you have already created a Web Service and hosted it in a
virtual directory called stockquote. i.e. the full path to the Web Service is
http://dotnet101.com/stockquote/stockquote.asmx.
Also we shall be using VS.NET to build our Win Form.
Building a Win Form Application
Create a new Project
Start-up VS.NET and Start a new Project from File ->New ->Project ->Visual C# Projects->Windows Application. In the Name field for the Application enter StockQuoteConsume. The Design View of VS.NET changes as shown below.
Figure 1
Adding Components
Now drop a TextBox, a Button and a ListBox on the Win Form in Design view from the Toolbox as shown below.
Figure 2
Adding a Web Reference
To consume a Web Service from VS.NET you have to add a Web Reference to the Web Service. To add a Web Reference to our Stock-Quotes Web Service, go to the Project menu -> Add Web Reference. A dialog box which shows up is shown below. Enter the Location (URI) path to the Stock-Quotes Web Service in the Address textbox and click Enter. Once VS.NET discovers the Web Service click Add Reference to add a Web Reference to the Stock-Quotes to our Project. While adding Web Reference, VS.NET automatically generates all the necessary plumbing code like Proxy class generation etc required to consume the Web Service
Figure 3
The Solution Window gets updated like below to reflect the
Web Service reference added.
Figure 4
Wire-Up the Button Event-Handler
Double-Click on the button on the Win Form in design window to write the event handling code for the button. The event handling code for the button is as under.
protected void button1_Click (object sender, System.EventArgs e)
{
//Check if the TextBox has some
text
if(textBox1.Text!=null)
{
//Clear the
ListBox
listBox1.Items.Clear();
//Create a Instance of the Autogenerated Proxy
class
localhost.DailyStock ds
= new localhost.DailyStock();
//Call the WebMethod GetQuote and
pass
//the symbol from the
TextBox
string results =
ds.GetQuote(textBox1.Text);
// The returned string has values which are separated
// by
commas.
// Hence we split the
returned string into parts
char[] splitter = {','} ;
string[] temp = results.Split(splitter);
// Check if the string array returned has more than
15
// elements since if there
are less than 15 elements
//
then an exception must have been returned
if(temp.Length>15)
{
//Put all the values in the
ListBox
//The order of this is based on the order of Items
//returned by Yahoo's service
listBox1.InsertItem(0,"Stock Quotes Web Service from
.NET101");
listBox1.InsertItem(1,"Symbol
:"+temp[0]);
listBox1.InsertItem(2,"Current Index
:"+temp[1]);
listBox1.InsertItem(3,"Date/Time
:"+temp[2]+":"+temp[3]);
listBox1.InsertItem(4,"Change
:"+temp[4]);
listBox1.InsertItem(5,"Open
:"+temp[5]);
listBox1.InsertItem(6,"High
:"+temp[6]);
listBox1.InsertItem(7,"Low
:"+temp[7]);
listBox1.InsertItem(8,"Volume
:"+temp[8]);
listBox1.InsertItem(9,"Market Capitalization
:"+temp[9]);
listBox1.InsertItem(10,"Previous Close
:"+temp[10]);
listBox1.InsertItem(11,"Percentage Change
:"+temp[11]);
listBox1.InsertItem(12,"Range
:"+temp[13]);
listBox1.InsertItem(13,"Earning Per Share
:"+temp[14]);
listBox1.InsertItem(14,"Price Earning Ratio
:"+temp[15]);
listBox1.InsertItem(15,"Company Name
:"+temp[16]);
}
else
{
//This is a error message
listBox1.InsertItem(0,"Error: Getting
Stock-Quote");
}
}
}
Compile the Project
Save the project and
select Build Menu-> Build to compile the Project. Once compiled press Ctrl+F5
to run the Win Form Application. This completes the Win Form Consumer for our
Stock Quotes Web Service.
Figure 5
- 1换个角度切苹果
- 2知识库建设应规避的5点具体误区
- 3泛普软件如何实现知识库双机热备
- 4石家庄OA信息化项目的“做”与“不做”(BY AMT 夏敬华)
- 5Re: 知识时代企业的四个要素
- 6如何画石家庄OA信息化项目实施方法论这幅地图(by AMT夏敬华孔祥云)
- 7Web服务 按需服务——Sun ONE为企业建立服务驱动型网络
- 8TIBCO Web Service为OSS/BSS搭建强大平台
- 9美国三大IT巨头将向OASIS提交Web服务安全标准
- 10从Web Services中访问服务器变量
- 11W3C发表WSDL 1.2规范
- 12理解Web服务的服务质量
- 13两种对立标准联姻 微软IBM规范Web服务
- 14APQC是如何看石家庄OA信息化的?
- 15一波“三折”:我的OA选型经历(下)
- 16Licensing
- 17加速战略学习
- 18初探石家庄OA信息化
- 19Borland加快Web服务的分发速度
- 20协同之惑
- 212008协同软件冰火之年:概念褪去 普及延伸
- 22Web服务设计师,第6部分:基于付费的Web服务的催化剂
- 23在长时间操作过程中更新显示
- 24Web服务内幕,第8部分:关于Soap的决策
- 25泛普软件石家庄OA信息化实施阶段划分
- 26资本的冬天是协同软件行业的春天
- 27协同办公OA软件对系统登陆的安全设置
- 28借助RDF增强WSDL--管理结构化的Web服务元数据
- 29专家称XML Web服务时代正接近尾声
- 30架构Web Service:描述与注册,发布Web服务
成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼