web 建立 Web應用程式使用者<br>
<br>
下面建立一個Web應用程式StockConsumer.aspx,它作為這個StockQuote(股票報價) Web服務的第一個使用者。 <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 服務DailyStock進行例示。由於已經產生了代理庫,因此Web服務的調用方法與其它任何庫的調用方法都相同。調用DailyStock 類的GetQuote()方法後,將返回一個字串,其中包含了以逗號分隔的列表符號的完整資訊。<br>
<br>
我們將限制顯示給客戶的資訊為只顯示當前指數和所報告指數的日期/時間。為了將字串分成若干不同的部分,這裡使用了字串類的Split方法,在出現逗號的地方將字串分割成部分。並且,將分割開的字串組成數組之後,再使用相關的數值為Web版面設定不同的標籤。 <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>