/* This sample demonstrates use of the PowerTCP WebASP 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; using System.Diagnostics; namespace HttpStockQuote { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button cmdGetQuote; protected System.Web.UI.WebControls.TextBox txtResult; protected System.Web.UI.WebControls.TextBox txtSymbol; // http control protected DartWebASP.WebASP WebAsp1 = new DartWebASP.WebASPClass(); 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) { if(txtSymbol.Text == "") txtResult.Text = "Unable to retrieve quote, please enter a symbol."; else { // retrieve yahoo finance info, querystring tells yahho which symbol to grab WebAsp1.Request.Url = "http://finance.yahoo.com/q"; WebAsp1.Request.UrlQueryString ="s=" + txtSymbol.Text + "&d=v1"; try { WebAsp1.Get(); txtResult.Text = ParsePage(WebAsp1.Response.Body.ReadString(0), txtSymbol.Text); } catch(Exception GetError) { txtResult.Text = "Get Error: " + GetError.Message; } } } 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; } } }