|
|
|
Imports System |
|
Imports System.Collections.Generic |
|
Imports System.ComponentModel |
|
Imports System.Data |
|
Imports System.IO |
|
Imports System.Net |
|
Imports System.Text |
|
Imports System.Web |
|
Imports System.Web.Services |
|
Imports System.Xml |
|
Imports System.Globalization |
|
|
|
|
|
<WebService([Namespace]:="http://www.telerik.com/"), WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ |
|
Public Class StockQuoteService |
|
Inherits WebService |
|
Private random As Random |
|
Private yahooFeedTemplate As String = "http://finance.yahoo.com/d/quotes.csv?s={0}&f=nl1d1t1c1ohgv&e=.csv" |
|
|
|
Public Sub New() |
|
random = New Random(DateTime.Now.Millisecond) |
|
End Sub |
|
|
|
Property SessionQuotesData() As String |
|
Get |
|
Return HttpContext.Current.Session("TelerikAspAjaxGridApplication") |
|
End Get |
|
Set(ByVal value As String) |
|
HttpContext.Current.Session("TelerikAspAjaxGridApplication") = value |
|
End Set |
|
End Property |
|
|
|
<WebMethod(), Description("Gets stock quote for a specific symbol")> _ |
|
Public Function GetStockQuotes(ByVal stockTickers As String) As DataSet |
|
Dim stockTickerList As String() = stockTickers.Split(","c) |
|
Dim index As Integer |
|
For index = 0 To stockTickerList.Length - 1 |
|
stockTickerList(index) = stockTickerList(index).Trim() |
|
Next index |
|
|
|
Dim quotes As List(Of Quote) = New List(Of Quote) |
|
|
|
Dim url As String = String.Empty |
|
Dim webRequest As WebRequest = Nothing |
|
Dim webResponse As WebResponse = Nothing |
|
Dim stockTicker As String |
|
For Each stockTicker In stockTickerList |
|
url = String.Format(yahooFeedTemplate, HttpUtility.UrlEncode(stockTicker)) |
|
webRequest = HttpWebRequest.Create(url) |
|
webResponse = webRequest.GetResponse() |
|
|
|
Dim quote As Quote = GetQuoteData(webResponse.GetResponseStream()) |
|
quotes.Add(quote) |
|
Next stockTicker |
|
|
|
|
|
SerializeQuotesData(quotes) |
|
|
|
Dim dataset As New DataSet("QuotesDataSet") |
|
Dim data As XmlReader = XmlReader.Create(New System.IO.StringReader(SessionQuotesData)) |
|
dataset.ReadXml(data) |
|
Return dataset |
|
End Function |
|
|
|
|
|
|
|
|
|
Private Overloads Function GetQuote(ByVal bufferList() As String) As Quote |
|
Dim stockTicker As String = bufferList(0) |
|
Dim stockQuote As [Decimal] = [Decimal].Parse(bufferList(1), CultureInfo.InvariantCulture) |
|
Dim lastUpdated As DateTime = DateTime.Parse((bufferList(2) + " " + bufferList(3)), CultureInfo.InvariantCulture) |
|
Dim change As [Decimal] = [Decimal].Parse(bufferList(4), CultureInfo.InvariantCulture) |
|
Dim dailyMaxRange As [Decimal] = 0D |
|
If (bufferList(6) <> "N/A") Then |
|
dailyMaxRange = [Decimal].Parse(bufferList(6), CultureInfo.InvariantCulture) |
|
End If |
|
|
|
Dim dailyMinRange As [Decimal] = 0D |
|
If (bufferList(7) <> "N/A") Then |
|
dailyMinRange = [Decimal].Parse(bufferList(7), CultureInfo.InvariantCulture) |
|
End If |
|
|
|
Return New Quote(stockTicker, stockQuote, lastUpdated, change, dailyMinRange, dailyMaxRange) |
|
End Function |
|
|
|
|
|
Private Overloads Function GetQuote(ByVal quoteNode As XmlNode) As Quote |
|
|
|
Dim stockTicker As String = quoteNode.Attributes("stockTicker").Value |
|
Dim stockQuote As [Decimal] = [Decimal].Parse(quoteNode.Attributes("lastTrade").Value, CultureInfo.InvariantCulture) |
|
Dim lastUpdated As DateTime = DateTime.Parse(quoteNode.Attributes("lastUpdated").Value, CultureInfo.InvariantCulture) |
|
Dim change As [Decimal] = [Decimal].Parse(quoteNode.Attributes("change").Value, CultureInfo.InvariantCulture) |
|
Dim dailyMaxRange As [Decimal] = IIf(quoteNode.Attributes("dailyMaxRange").Value = "N/A", 0D, [Decimal].Parse(quoteNode.Attributes("dailyMaxRange").Value, CultureInfo.InvariantCulture)) |
|
Dim dailyMinRange As [Decimal] = IIf(quoteNode.Attributes("dailyMinRange").Value = "N/A", 0D, [Decimal].Parse(quoteNode.Attributes("dailyMinRange").Value, CultureInfo.InvariantCulture)) |
|
|
|
Return New Quote(stockTicker, stockQuote, lastUpdated, change, dailyMinRange, dailyMaxRange) |
|
End Function |
|
|
|
|
|
Private Function GetQuoteData(ByVal dataStream As Stream) As Quote |
|
Dim buffer As String = Nothing |
|
Dim sr As New StreamReader(dataStream) |
|
Try |
|
buffer = sr.ReadToEnd() |
|
Finally |
|
sr.Dispose() |
|
End Try |
|
|
|
buffer = buffer.Replace("""", "") |
|
buffer = buffer.Replace(ControlChars.Cr + ControlChars.Lf, "") |
|
|
|
Dim bufferList As String() = buffer.Split(New Char() {","c}) |
|
Dim quote As Quote = GetQuote(bufferList) |
|
|
|
Return quote |
|
End Function |
|
|
|
Private Function GetQuotesData(ByVal xmlData As String) As List(Of Quote) |
|
Dim xmlDocument As New XmlDocument() |
|
xmlDocument.LoadXml(xmlData) |
|
|
|
Dim quotes As List(Of Quote) = New List(Of Quote) |
|
Dim quote As Quote = Nothing |
|
Dim quoteNode As XmlNode |
|
For Each quoteNode In xmlDocument.DocumentElement.ChildNodes |
|
quote = GetQuote(quoteNode) |
|
quotes.Add(quote) |
|
Next quoteNode |
|
|
|
Return quotes |
|
End Function |
|
|
|
|
|
Private Sub SerializeQuotesData(ByVal quotes As List(Of Quote)) |
|
Dim str As StringBuilder = New StringBuilder("<quotes>") |
|
Try |
|
Dim quote As Quote |
|
For Each quote In quotes |
|
str.Append("<quote ") |
|
str.Append("stockTicker=""" & quote.StockTicker & """ ") |
|
str.Append("lastTrade=""" & quote.StockQuote.ToString(CultureInfo.InvariantCulture) & """ ") |
|
str.Append("change=""" & quote.Change.ToString(CultureInfo.InvariantCulture) & """ ") |
|
str.Append("lastUpdated=""" & quote.LastUpdated.ToString("MM/dd/yyyy HH:mm:ss") & """ ") |
|
str.Append("dailyMaxRange=""" & quote.DailyMaxRange.ToString(CultureInfo.InvariantCulture) & """ ") |
|
str.Append("dailyMinRange=""" & quote.DailyMinRange.ToString(CultureInfo.InvariantCulture) & """ ") |
|
str.Append(" />") |
|
Next quote |
|
Catch e As Exception |
|
End Try |
|
str.Append("</quotes>") |
|
SessionQuotesData = str.ToString() |
|
End Sub |
|
End Class |