/* This sample demonstrates use of the PowerTCP TCP Component in the ASP.NET environment. * This sample gets a page from Yahoo's stock service, passing a querystring in the request. * Yahoo uses this querystring to render information about the stock symbol in the response. * This response is then parsed to obtain the stock value */ using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace TcpStockQuote { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtSymbol; protected System.Web.UI.WebControls.Button cmdGetQuote; protected System.Web.UI.WebControls.TextBox txtResults; protected DartSock.Tcp Tcp1 = new DartSock.TcpClass(); public WebForm1() { Page.Init += new System.EventHandler(Page_Init); } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.cmdGetQuote.Click += new System.EventHandler(this.cmdGetQuote_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void cmdGetQuote_Click(object sender, System.EventArgs e) { // first retrieve the page, then parse the results string page = ""; if(GetPage(txtSymbol.Text, ref page)) { txtResults.Text = ParsePage(page, txtSymbol.Text); } } private bool GetPage(string Symbol, ref string page) { Tcp1.Timeout = 5000; bool connected = false; bool success = false; try { // make an HTTP connection Tcp1.Connect("finance.yahoo.com", 80, "", 0); connected = true; } catch(Exception ConnectError) { txtResults.Text = "Connect Error: " + ConnectError.Message; } if(connected) { object data = new Object(); // request the page, passing the symbol in the querystring data = "GET /q?s=" + Symbol + "&d=v1 HTTP/1.0" + "\r\n" + "\r\n"; try { Tcp1.Send(ref data, 0, false); object header = new Object(); object token = new Object(); // header comes first header = ""; token = "\r\n" + "\r\n"; Tcp1.Search(ref header, ref token); data = ""; // keep receiving data until the connection is closed do { Tcp1.Receive(ref data); page = page + data.ToString(); } while(Tcp1.State != 0); success=true; } catch(Exception SendError) { txtResults.Text = "Error: " + SendError.Message; } } return success; } private string ParsePage(string page, string symbol) { // parse the page to get the quote string token = ""; string results = ""; int pos1; int pos2; bool changeup=false; // Look for direction image if(page.IndexOf("up_g.gif") > -1) changeup = true; token = ""; pos1 = page.IndexOf(token); if(pos1 > -1) { pos1 = pos1 + token.Length; pos2 = page.IndexOf("<", pos1); results += "Company: " + page.Substring(pos1, pos2 - pos1) + "\r\n"; token = "Last Trade:"; pos1 = page.IndexOf(token); if(pos1 > -1) { pos1 = pos1 + token.Length; pos2 = page.IndexOf("<", pos1); results += "Last Trade: " + page.Substring(pos1, pos2 - pos1) + "\r\n"; token = "Change:"; pos1 = page.IndexOf(token); if(pos1 > -1) { pos1 = pos1 + token.Length; token = ";\">"; pos1 = page.IndexOf(token, pos1); if(pos1 > -1) { pos1 = pos1 + token.Length; pos2 = page.IndexOf("<", pos1); results += "Change: "; if(changeup) results += "+"; else results += "-"; results += page.Substring(pos1, pos2 - pos1) + "\r\n"; } else results += "Error: change info not found."; } else results += "Error: change info not found."; } else results += "Error: trade info not found."; } else results += "Error: company not found."; return results; } } }