国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

股票報價的WebService(轉天極網)之一

2024-07-21 02:21:31
字體:
來源:轉載
供稿:網友
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;
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉城县| 三河市| 大厂| 皋兰县| 安泽县| 朝阳市| 深水埗区| 巴楚县| 吴川市| 米泉市| 宝兴县| 五寨县| 灌南县| 全南县| 安丘市| 定陶县| 黄山市| 米脂县| 无棣县| 库尔勒市| 腾冲县| 湖口县| 扶风县| 明水县| 鄄城县| 嘉禾县| 白银市| 休宁县| 松江区| 瑞昌市| 大洼县| 蒙自县| 勐海县| 广德县| 攀枝花市| 德格县| 临泉县| 长沙市| 清苑县| 三门县| 新晃|