This should get you started:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Xml;
using System.Net;
using System.IO;
namespace daniweb
{
public partial class frmStockRSS : Form
{
public frmStockRSS()
{
InitializeComponent();
}
private static StockInfo GetStockInfo(string StockTicker)
{
if (string.IsNullOrEmpty(StockTicker))
return null;
const string yhoo_url = @"http://download.finance.yahoo.com/d/quotes.csv?s={0}&f=sl1d1t1c1ohgv&e=.csv";
string url = string.Format(yhoo_url, HttpUtility.UrlEncode(StockTicker.ToUpper()));
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "GET";
req.Referer = string.Empty;
HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();
string resp = default(string);
try
{
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
resp = sr.ReadToEnd();
sr.Close();
}
}
catch (HttpException)
{
return null;
}
catch (IOException)
{
return null;
}
StockInfo result = StockInfo.FromYahoo(resp);
return result;
}
private void button1_Click(object sender, EventArgs e)
{
StockInfo info = GetStockInfo("GOOG");
Console.WriteLine(info.Ticker + ": " + info.LastTrade);
info = GetStockInfo("YHOO");
Console.WriteLine(info.Ticker + ": " + info.LastTrade);
}
}
public class StockInfo
{
private string _ticker;
private decimal _lastTrade;
private DateTime _tickerTime;
private decimal _change;
private decimal _open;
private decimal _high;
private decimal _low;
private int _unknown;
public string Ticker
{
get { return _ticker; }
set { _ticker = value; }
}
public decimal LastTrade
{
get { return _lastTrade; }
set { _lastTrade = value; }
}
public DateTime TickerTime
{
get { return _tickerTime; }
set { _tickerTime = value; }
}
public decimal Change
{
get { return _change; }
set { _change = value; }
}
public decimal Open
{
get { return _open; }
set { _open = value; }
}
public decimal High
{
get { return _high; }
set { _high = value; }
}
public decimal Low
{
get { return _low; }
set { _low = value; }
}
public int Unknown
{
get { return _unknown; }
set { _unknown = value; }
}
private StockInfo()
{
}
public static StockInfo FromYahoo(string s)
{
if (string.IsNullOrEmpty(s))
return null;
s = s.Replace("\"", string.Empty);
string[] vals = s.Split(new char[] { ',' }, StringSplitOptions.None);
if (vals.Length != 9)
return null;
StockInfo result = new StockInfo();
result.Ticker = vals[0];
result.LastTrade = Convert.ToDecimal(vals[1]);
result.TickerTime = Convert.ToDateTime(vals[2] + " " + vals[3]); //This is very unsafe.
result.Change = Convert.ToDecimal(vals[4]);
result.Open = Convert.ToDecimal(vals[5]);
result.High = Convert.ToDecimal(vals[6]);
result.Low = Convert.ToDecimal(vals[7]);
result.Unknown = Convert.ToInt32(vals[8]); //dont know what this is
return result;
}
}
}
Results in:
Reputation Points: 1749
Solved Threads: 735
Senior Poster
Offline 3,948 posts
since Feb 2009