您的位置:建站学院首页 >> 文章 >> asp.net >> WebService
股票报价的WebService

构造代理库

  .NET 平台上的应用程序可以使用代理库来调用Web服务上的方法,这样使用Web服务就非常容易。生成代理库的第一步是从SDL中生成一个Web服务的代理类。.NET SDK提供了一个叫做WebServiceUtil.exe的工具,它能够帮助我们生成一个代理类。要生成Web服务的代理类,首先进入命令行环境,然后转到将要开发客户应用程序的那个目录,接着输入以下命令:

  WebServiceUtil /c:proxy /pa:http://localhost/stockquote/StockQuote.asmx?SDL /n:Quotes

以上命令执行后,会在运行这个命令的目录中生成一个 C# 源代码文件,叫做 DailyStock.cs (要记住Web服务类的名称也是 DailyStock,)。现在来看这些自变量的含义:

  l /c:proxy:告诉WebServiceUtil生成一个代理类

  l /pa:http://localhost/stockquote/StockQuote.asmx?SDL:向SDL提供Web服务的路径。如果已经将Web服务的SDL 保存到了硬盘上,我们甚至可以提供SDL文件的本地路径。

  l /n:Quotes:告诉WebServiceUtil将代理类放在 Quotes名称空间。这样做的目的是为了更容易地管理和使用代理库。

  代理库准备好之后,我们使用C# 编译器从上面步骤中刚创建的代理类中生成一个代理库:

  csc /target:library /r:System.dll;System.Web.Services.dll;System.Net.dll;
  System.IO.dll;System.Xml.Serialization.dll DailyStock.cs

生成代理库的时候,我们使用了 /target:library开关以表示想要输出一个库文件。我们还引用一些曾经在Web服务中使用过的集合。编译器将在运行命令所在目录中生成一个名为DailyStock.dll 的dll 库。

创建 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>

  下面总结一下配置Web应用程序的步骤:

  l 创建一个叫做StockConsumer的虚拟目录

  l 将 StockConsumer.aspx 文件复制到这个虚拟目录下

  l 在wwwroot 文件夹中创建一个 bin 虚拟目录

  l 设置bin目录以执行代码的权限

  l 将代理 dll 文件DailyStock.dll复制到这个目录下,ASP.NET 运行时间引擎会自动从Bin目录中选择对外部库的引用。这里的例子中,这个外部库是DailyStock.dll。

  现在调用文件 http://localhost/StockConsumer/StockConsumer.aspx,然后键入股票的符号,点击 "Get Quote"(获取报价)按钮,就能看到正在使用的Web服务了。注意:Web服务应该能够使用yahoo服务器,否则它就会返回一个错误信息。

下面创建一个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>

  下面总结一下配置Web应用程序的步骤:

  l 创建一个叫做StockConsumer的虚拟目录

  l 将 StockConsumer.aspx 文件复制到这个虚拟目录下

  l 在wwwroot 文件夹中创建一个 bin 虚拟目录

  l 设置bin目录以执行代码的权限

  l 将代理 dll 文件DailyStock.dll复制到这个目录下,ASP.NET 运行时间引擎会自动从Bin目录中选择对外部库的引用。这里的例子中,这个外部库是DailyStock.dll。

  现在调用文件 http://localhost/StockConsumer/StockConsumer.aspx,然后键入股票的符号,点击 "Get Quote"(获取报价)按钮,就能看到正在使用的Web服务了。注意:Web服务应该能够使用yahoo服务器,否则它就会返回一个错误信息。

本内容共2页  首 页  上一页  下一页  尾 页  当前在第2

Google
 
Web www.cqxw.net