欢迎光临
我们一直在努力

股票报价的WebService(转天极网)之一-.NET教程,Web Service开发

建站超值云服务器,限时71元/月

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=&quot;c#&quot; class=&quot;dailystock&quot; %><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 = @&quot;http://quote.yahoo.com/d/quotes.csv?s=&quot;+symbol+&quot;&f=sl1d1t1c1ohgvj1pp2owern&e=.csv&quot;; <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=&quot;exception occurred&quot; ;<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的输出信息返回给用户。&nbsp;

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 股票报价的WebService(转天极网)之一-.NET教程,Web Service开发
分享到: 更多 (0)