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