當(dāng)前位置:工程項目OA系統(tǒng) > 泛普各地 > 河北O(jiān)A系統(tǒng) > 石家莊OA系統(tǒng) > 石家莊OA信息化
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知識庫建設(shè)應(yīng)規(guī)避的5點具體誤區(qū)
- 3泛普軟件如何實現(xiàn)知識庫雙機熱備
- 4石家莊OA信息化項目的“做”與“不做”(BY AMT 夏敬華)
- 5Re: 知識時代企業(yè)的四個要素
- 6如何畫石家莊OA信息化項目實施方法論這幅地圖(by AMT夏敬華孔祥云)
- 7Web服務(wù) 按需服務(wù)——Sun ONE為企業(yè)建立服務(wù)驅(qū)動型網(wǎng)絡(luò)
- 8TIBCO Web Service為OSS/BSS搭建強大平臺
- 9美國三大IT巨頭將向OASIS提交Web服務(wù)安全標(biāo)準(zhǔn)
- 10從Web Services中訪問服務(wù)器變量
- 11W3C發(fā)表WSDL 1.2規(guī)范
- 12理解Web服務(wù)的服務(wù)質(zhì)量
- 13兩種對立標(biāo)準(zhǔn)聯(lián)姻 微軟IBM規(guī)范Web服務(wù)
- 14APQC是如何看石家莊OA信息化的?
- 15一波“三折”:我的OA選型經(jīng)歷(下)
- 16Licensing
- 17加速戰(zhàn)略學(xué)習(xí)
- 18初探石家莊OA信息化
- 19Borland加快Web服務(wù)的分發(fā)速度
- 20協(xié)同之惑
- 212008協(xié)同軟件冰火之年:概念褪去 普及延伸
- 22Web服務(wù)設(shè)計師,第6部分:基于付費的Web服務(wù)的催化劑
- 23在長時間操作過程中更新顯示
- 24Web服務(wù)內(nèi)幕,第8部分:關(guān)于Soap的決策
- 25泛普軟件石家莊OA信息化實施階段劃分
- 26資本的冬天是協(xié)同軟件行業(yè)的春天
- 27協(xié)同辦公OA軟件對系統(tǒng)登陸的安全設(shè)置
- 28借助RDF增強WSDL--管理結(jié)構(gòu)化的Web服務(wù)元數(shù)據(jù)
- 29專家稱XML Web服務(wù)時代正接近尾聲
- 30架構(gòu)Web Service:描述與注冊,發(fā)布Web服務(wù)
成都公司:成都市成華區(qū)建設(shè)南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓
版權(quán)所有:泛普軟件 渝ICP備14008431號-2 渝公網(wǎng)安備50011202501700號 咨詢電話:400-8352-114