扫二维码关注kmpro
KMPRO
站内搜索
 位置:首页>知识管理实施>IT实施阶段>

Consuming a Web Service from a Win Form Application 来源:   时间:2002-09-13 11:09   作者:AMTeam.org

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

关键词:
相关文章
分享到:
2010-2012 北京深蓝海域信息科技有限公司
Darkblue Sea Software Co., Ltd. all rights reserved
京ICP备13004508号-1
地址:北京市昌平区北清路1号珠江摩尔国际中心6号楼1单元907单位
电话:010-89146616/82969378
传真:010-82969378
公司介绍 | 人才招募 | 网站地图 | 联系我们 | 企业资质