Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using Quotes;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace WebApplication1
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- //change stockName value to get quotes for different comapny
- string stockName = "brcm";
- StockPrice sp = FetchStockQuotes(stockName);
- Console.WriteLine("Stocks for " + sp.Name + " : " + sp.TradeRate + " at time " + sp.TradeDate);
- string a = sp.Name;
- string b = sp.TradeRate.ToString();
- string c = sp.TradeDate;
- TextBox1.Text = a;
- TextBox2.Text = b;
- TextBox3.Text = c;
- }
- private static List<StockPrice> ParseStockPrices(string csvData)
- {
- List<StockPrice> prices = new List<StockPrice>();
- if (csvData != null && csvData != "")
- {
- try
- {
- string[] rows = csvData.Replace("\r", "").Split('\n');
- foreach (string row in rows)
- {
- if (string.IsNullOrEmpty(row)) continue;
- string[] cols = row.Split(',');
- StockPrice p = new StockPrice();
- p.Symbol = cols[0].Replace("\"", "");
- p.Name = cols[1].Replace("\"", ""); ;
- p.TradeRate = Convert.ToDecimal(cols[2].Replace("\"", ""));
- p.TradeDate = cols[3].ToString().Replace("\"", "") + " " + cols[4].ToString().Replace("\"", "").ToUpper() + " EST";
- p.Change = Convert.ToDecimal(cols[5]);
- p.ChangePercent = Convert.ToString(cols[6]).Replace("\"", "");
- p.ChangePercent = p.ChangePercent.Replace(" - ", "*");
- string[] temp = p.ChangePercent.Split('*');
- p.ChangePercent = temp[1].Replace("\"", "");
- prices.Add(p);
- }
- }
- catch (Exception ex)
- {
- }
- }
- return prices;
- }
- private static string GetStockMarketUrl()
- {
- return "http://finance.yahoo.com/d/quotes.csv?f=snl1d1t1c1c&s=";
- //return StockMarketURL;
- }
- public static StockPrice FetchStockQuotes(string symbol)
- {
- string url = GetStockMarketUrl() + symbol;
- string csvData = string.Empty;
- List<StockPrice> prices = new List<StockPrice>();
- using (WebClient web = new WebClient())
- {
- try
- {
- csvData = web.DownloadString(url);
- if (csvData != "")
- {
- prices = ParseStockPrices(csvData);
- }
- }
- catch (Exception ex)
- {
- }
- }
- return prices[0];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement