首页 > 开发 > 综合 > 正文

利用WebService制作股票报价4

2020-02-03 16:41:14
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表