股票報價的WebService(轉(zhuǎn)天極網(wǎng))之四
2024-07-21 02:21:32
供稿:網(wǎng)友
創(chuàng)建 web應(yīng)用程序用戶<br>
<br>
下面創(chuàng)建一個web應(yīng)用程序stockconsumer.aspx,它作為這個stockquote(股票報價) web服務(wù)的第一個用戶。 <br>
<br>
<%@ page language="c#" %><br>
<%@ import namespace="system.xml" %><br>
<%@ import namespace="quotes" %><br>
<br>
以上引入必要的名稱空間。要記住也要引入 quotes名稱空間,它是代理庫的名稱空間。<br>
<br>
<html><br>
<head><br>
<script runat=server><br>
// wire up the onclick event for a button <br>
protected void button1_click(object sender, eventargs e)<br>
{<br>
file://create a object of the class dailystock (the proxy class)<br>
dailystock ds = new dailystock();<br>
<br>
// call the getquote method of the proxy class dailystock and <br>
// pass the symbol string from the textbox <br>
string res = ds.getquote(symbol.text);<br>
<br>
// the returned string has values which are separated <br>
// by commas.<br>
// hence we split the returned string into parts <br>
char[] splitter = {','} ;<br>
string[] temp = res.split(splitter);<br>
<br>
// check if the string array returned has more than one <br>
// elements since if there are less than one elements <br>
// then an exception must have been returned <br>
if(temp.length >1)<br>
{<br>
// the webservice returns a lot of information about the<br>
// stock. we only show the relevant portions<br>
// set the label to current index<br>
curindex.text = "current index :"+temp[1]; <br>
<br>
// set the label to current date time<br>
curdate.text ="last update on"+temp[2]+" at "+temp[3]; <br>
}<br>
else<br>
{<br>
error.text = "error :"+res ; file://set the error label<br>
}<br>
}<br>
</script><br>
<br>
以上asp.net頁面代碼中,首先對web 服務(wù)dailystock進行例示。由于已經(jīng)生成了代理庫,因此web服務(wù)的調(diào)用方法與其它任何庫的調(diào)用方法都相同。調(diào)用dailystock 類的getquote()方法后,將返回一個字符串,其中包含了以逗號分隔的列表符號的完整信息。<br>
<br>
我們將限制顯示給客戶的信息為只顯示當前指數(shù)和所報告指數(shù)的日期/時間。為了將字符串分成若干不同的部分,這里使用了字符串類的split方法,在出現(xiàn)逗號的地方將字符串分割成部分。并且,將分割開的字符串組成數(shù)組之后,再使用相關(guān)的數(shù)值為web頁面設(shè)置不同的標簽。 <br>
<br>
代碼的其余部分<br>
<br>
<body><br>
<center><br>
<h2>.net101 stock quote consumer </h2><br>
<br>
<form runat=server ><br>
<table border=1 celspacing=1><br>
<tr><th>please enter the symbol below</th></tr><br>
<tr><td><br>
<asp:textbox id=symbol runat=server /> <br>
<asp:button id=button1 text="get quote" onclick="button1_click" runat=server /><br>
</td></tr><br>
<tr><td><asp:label id=curindex runat=server /></td></tr><br>
<tr><td><asp:label id=curdate runat=server /></td></tr><br>
<tr><td><asp:label id=error runat=server /></td></tr><br>
</table><br>
</form><br>
<br>
</center><br>
</body><br>
</html><br>