日期:2012-09-26  浏览次数:20517 次

Web Services,即Web服务,是微软.NET战略中非常重要的一个概念。它的目的是将Web站点转变为集组织、应用、服务以及设备于一体的可设计Web站点,使Web站点不再处于被动的地位。<br>
<br>
  本文将介绍如何建立和使用一个在.NET 平台上提供股票报价的Web服务。我们将使用Yahoo的一项以CSV(以逗号分隔的值)的格式提供股票报价的免费服务,将其包含在我们的web 服务中。<br>
<br>
  注意:这个报价服务例程的运行大约延迟15分钟,只用于教学目的。 <br>
<br>
  建立Web服务<br>
<br>
  下面将采用逐步讲解代码的形式来帮助你理解在.NET 中Web服务的编程模式。我们可以使用notepad等任何文本编辑器来编写 这里的Web服务例程代码,最后将文件存储为StockQuote.asmx。请注意:所有的Web服务文件保存时都使用扩展名 *.asmx。<br>
<br>
  <%@ WebService Language="C#" class="DailyStock" %><br>
<br>
代码的第一行定义了一个 Web 服务,使用的语言是C#。class属性用来指示Web服务应该调用和使用的类。如果在Web服务中使用了许多类,那么就应该用这个属性来表明Web服务应该首先调用的类。<br>
<br>
  using System ;<br>
  using System.Web.Services ;<br>
  using System.Net ;<br>
  using System.IO ;<br>
  using System.Text ;<br>
<br>
以上代码负责引入必要的名称空间。 请记住永远都要引入System.Web.Services这个名称空间 。根据类的需要,再引入保留的名称空间。 <br>
<br>
  public class DailyStock : WebService<br>
   {<br>
    ......<br>
    ....<br>
   }<br>
<br>
这里我们将公共类定义为 DailyStock,它扩展了 System.Web.Services.WebService 类。所有想暴露为 Web服务的类都应该扩展System.Web.Services.WebServices类。 另外,Web 服务的存取修饰语永远都是public。<br>
<br>
  [WebMethod]<br>
   public string GetQuote(string symbol)<br>
   {<br>
    ........<br>
    ........<br>
   }<br>
<br>
以上我们定义了一个公共Web方法 GetQuote。同类的定义一样,Web 方法也都要用 public这个修饰语来声明。 [WebMethod] 属性呈现出将要被用在Web服务中的一些潜在方法,希望客户存取的所有方法都应该用[WebMethod] 属性进行标记。GetQuote方法接受一个字符串输入参数,它包含了使用者所需要的报价符号。这个方法返回一个字符串,其中包含股票报价或错误信息。 <br>
<br>
  string ret;<br>
   try<br>
    {<br>
     // The Path to the Yahoo Quotes Service<br>
     string fullpath = @"http://quote.yahoo.com/d/quotes.csv?s="+symbol+"&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"; <br>
<br>
     // Create a HttpWebRequest object on the Yahoo url<br>
<br>
     HttpWebRequest webreq = (HttpWebRequest)WebRequestFactory.Create(fullpath);<br>
<br>
     // Get a HttpWebResponse object from the Yahoo url<br>
<br>
     HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();<br>
<br>
     // Create a StreamReader object and pass the Yahoo Server stream as a parameter<br>
<br>
     StreamReader strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);<br>
<br>
     // Read a single line from the stream (from the server) <br>
     // We read only a single line, since the Yahoo server returns all the<br>
     // information needed by us in just one line.<br>
<br>
     ret= strm.ReadLine();<br>
<br>
     // Close the stream to the server and free the resources.<br>
<br>
     strm.Close();<br>
<br>
    }<br>
<br>
   catch(Exception)<br>
<br>
   {<br>
<br>
    // If exception occurred inform the user<br>
<br>
    ret="Exception Occurred" ;<br>
<br>
   }<br>
<br>
   file://Return the Quote or Exception<br>
<br>
   return ret ;<br>
<br>
  以上是GetQuote 方法的内容。这里使用一个 try-catch模块来截获从Yahoo中得到股票报价的过程中可能发生的错误。在 try-catch模块内部声明了一个字符串变量,这个变量中保存着获取yahoo服务的完整路径,用户提供的symbol字符串变量被加到这个连接字符串上。<br>
<br>
  路径建立好之后,就要从连接字符串中构造一个 HttpWebRequest对象和一个 HttpWebResponse 对象。接着,用StreamReader打开一个到Yahoo服务器的流。StreamReader 从服务器中读取一行, Yahoo提供给我们所需要的信息都是一行一行的。最后,流被关闭,Yahoo的输出信息返回给用户。