/// <summary> /// This method is called to log into the system and return /// a session id. Once you have the session ID you can call /// the search function to perform searches. /// </summary> /// <param name="uid">The user id to use for login.</param> /// <param name="pwd">The password to use for login.</param> /// <returns>The session id if login was successful, null if it was not.</returns> private String Login(String uid, String pwd) { Uri url = new Uri("http://www.httprecipes.com/1/8/cookieless.php"); WebRequest http = HttpWebRequest.Create(url); http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; Stream ostream = http.GetRequestStream(); FormUtility form = new FormUtility(ostream, null); form.Add("uid", uid); form.Add("pwd", pwd); form.Add("action", "Login"); form.Complete(); ostream.Close(); WebResponse response = http.GetResponse(); response.GetResponseStream(); String query = response.ResponseUri.Query; if (query != null) { NameValueCollection c = HttpUtility.ParseQueryString(query); return(c.Get("session")); } else { return(null); } }
/// <summary> /// This method builds a URL to load data from Oanda for a neural /// network to train with. /// </summary> /// <param name="ticker">The currency pair to access.</param> /// <param name="from">The begin date.</param> /// <param name="to">the ending date.</param> /// <returns>The URL to read from.</returns> private Uri buildURL( TickerSymbol ticker, DateTime from, DateTime to ) { // construct the url MemoryStream mstream = new MemoryStream(); FormUtility form = new FormUtility(mstream, null); String[] currencies = ticker.Symbol.Split('/'); // each param gets added individually as query parameter form.Add("exch", currencies[0].ToUpper()); form.Add("expr2", currencies[1].ToUpper()); form.Add("date1", from.ToString("MM-dd-yyyy")); form.Add("date2", to.ToString("MM-dd-yyyy")); form.Add("date_fmt", "us"); form.Add("lang", "en"); form.Add("margin_fixed", "0"); form.Add("SUBMIT", "Get+Table"); form.Add("format", "CSV"); form.Add("redirected", "1"); mstream.Close(); byte[] b = mstream.GetBuffer(); String str = "http://www.oanda.com/convert/fxhistory?" + StringUtil.FromBytes(b); return(new Uri(str)); }
/// <summary> /// This method is called to log into the system and establish /// the cookie. Once the cookie is established, you can call /// the search function to perform searches. /// </summary> /// <param name="uid">The user id to use for login.</param> /// <param name="pwd">The password to use for login.</param> /// <returns>True if the login was successful.</returns> private bool Login(String uid, String pwd) { Uri url = new Uri("http://www.httprecipes.com/1/8/cookie.php"); HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url); http.CookieContainer = cookies; http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; Stream ostream = http.GetRequestStream(); FormUtility form = new FormUtility(ostream, null); form.Add("uid", uid); form.Add("pwd", pwd); form.Add("action", "Login"); form.Complete(); ostream.Close(); HttpWebResponse response = (HttpWebResponse)http.GetResponse(); foreach (Cookie cookie in cookies.GetCookies(url)) { if (String.Compare(cookie.Name, "hri-cookie", true) == 0) { return(true); } } return(false); }
/// <summary> /// Upload a file. /// </summary> /// <param name="uid">The user id for the form.</param> /// <param name="pwd">The password for the form.</param> /// <param name="file">The file to upload.</param> public void Upload(String uid, String pwd, String file) { // Get the boundary used for the multipart upload. String boundary = FormUtility.getBoundary(); Uri url = new Uri("http://www.httprecipes.com/1/7/uploader.php"); WebRequest http = HttpWebRequest.Create(url); http.Timeout = 30000; // specify that we will use a multipart form http.ContentType = "multipart/form-data; boundary=" + boundary; http.Method = "POST"; Stream ostream = http.GetRequestStream(); // Construct a form. FormUtility form = new FormUtility(ostream, boundary); form.Add("uid", uid); form.Add("pwd", pwd); form.AddFile("uploadedfile", file); form.Complete(); ostream.Close(); // Perform the upload. WebResponse response = http.GetResponse(); Stream istream = response.GetResponseStream(); istream.Close(); }
/// <summary> /// Perform a Yahoo search. /// </summary> /// <param name="searchFor">What are we searching for.</param> /// <returns>The URLs that contain the specified item.</returns> public ICollection <Uri> Search(String searchFor) { ICollection <Uri> result = null; // build the Uri var mstream = new MemoryStream(); var form = new FormUtility(mstream, null); form.Add("appid", "YahooDemo"); form.Add("results", "100"); form.Add("query", searchFor); form.Complete(); var enc = new ASCIIEncoding(); String str = enc.GetString(mstream.GetBuffer()); mstream.Dispose(); var uri = new Uri( "http://search.yahooapis.com/WebSearchService/V1/webSearch?" + str); int tries = 0; bool done = false; while (!done) { try { result = DoSearch(uri); done = true; } catch (IOException e) { if (tries == 5) { throw; } Thread.Sleep(5000); } tries++; } return(result); }
/// <summary> /// This method builds a URL to load data from Yahoo Finance for a neural /// network to train with. /// </summary> /// <param name="ticker">The ticker symbol to access.</param> /// <param name="from">The beginning date.</param> /// <param name="to">The ending date.</param> /// <returns>The URL to read from</returns> private static Uri BuildURL(String ticker, DateTime from, DateTime to) { // construct the URL var mstream = new MemoryStream(); var form = new FormUtility(mstream, null); form.Add("s", ticker); form.Add("a", "" + (from.Month - 1)); form.Add("b", "" + from.Day); form.Add("c", "" + from.Year); form.Add("d", "" + (to.Month - 1)); form.Add("e", "" + to.Day); form.Add("f", "" + to.Year); form.Add("g", "d"); form.Add("ignore", ".csv"); mstream.Close(); byte[] b = mstream.GetBuffer(); String str = "http://ichart.finance.yahoo.com/table.csv?" + StringUtil.FromBytes(b); return(new Uri(str)); }
/// <summary> /// Navigate based on a form. Complete and post the form. /// </summary> /// <param name="form">The form to be posted.</param> /// <param name="submit">The submit button on the form to simulate clicking.</param> public void Navigate(Form form, Input submit) { try { #if logging if (logger.IsInfoEnabled) { logger.Info("Posting a form"); } #endif Stream istream; Stream ostream; Uri targetURL; WebRequest http = null; if (form.Method == Form.FormMethod.Get) { ostream = new MemoryStream(); } else { http = WebRequest.Create(form.Action.Url); http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; ostream = http.GetRequestStream(); } // add the parameters if present var formData = new FormUtility(ostream, null); foreach (DocumentRange dr in form.Elements) { if (dr is FormElement) { var element = (FormElement)dr; if ((element == submit) || element.AutoSend) { String name = element.Name; String value = element.Value; if (name != null) { if (value == null) { value = ""; } formData.Add(name, value); } } } } // now execute the command if (form.Method == Form.FormMethod.Get) { String action = form.Action.Url.ToString(); ostream.Close(); action += "?"; action += ostream.ToString(); targetURL = new Uri(action); http = WebRequest.Create(targetURL); var response = (HttpWebResponse)http.GetResponse(); istream = response.GetResponseStream(); } else { targetURL = form.Action.Url; ostream.Close(); var response = (HttpWebResponse)http.GetResponse(); istream = response.GetResponseStream(); } Navigate(targetURL, istream); istream.Close(); } catch (IOException e) { throw new BrowseError(e); } }
/// <summary> /// Access the website and perform a search for either states or capitals. /// </summary> /// <param name="search">A search string.</param> /// <param name="type">What to search for(s=state, c=capital)</param> public void Process(String search, String type) { String listType = "ul"; String listTypeEnd = "/ul"; StringBuilder buffer = new StringBuilder(); bool capture = false; // Build the URL. MemoryStream mstream = new MemoryStream(); FormUtility form = new FormUtility(mstream, null); form.Add("search", search); form.Add("type", type); form.Add("action", "Search"); form.Complete(); System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); String str = enc.GetString(mstream.GetBuffer()); String surl = "http://www.httprecipes.com/1/7/get.php?" + str; Uri url = new Uri(surl); WebRequest http = HttpWebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)http.GetResponse(); Stream istream = response.GetResponseStream(); ParseHTML parse = new ParseHTML(istream); // Parse from the URL. Advance(parse, listType, 0); int ch; while ((ch = parse.Read()) != -1) { if (ch == 0) { HTMLTag tag = parse.Tag; if (String.Compare(tag.Name, "li", true) == 0) { if (buffer.Length > 0) { ProcessItem(buffer.ToString()); } buffer.Length = 0; capture = true; } else if (String.Compare(tag.Name, "/li", true) == 0) { ProcessItem(buffer.ToString()); buffer.Length = 0; capture = false; } else if (String.Compare(tag.Name, listTypeEnd, true) == 0) { ProcessItem(buffer.ToString()); break; } } else { if (capture) { buffer.Append((char)ch); } } } }
/// <summary> /// This method will download an amortization table for the /// specified parameters. /// </summary> /// <param name="interest">The interest rate for the loan.</param> /// <param name="term">The term(in months) of the loan.</param> /// <param name="principle">The principle amount of the loan.</param> public void process(double interest, int term, int principle) { Uri url = new Uri("http://www.httprecipes.com/1/9/loan.php"); WebRequest http = HttpWebRequest.Create(url); http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; Stream ostream = http.GetRequestStream(); FormUtility form = new FormUtility(ostream, null); form.Add("interest", "" + interest); form.Add("term", "" + term); form.Add("principle", "" + principle); form.Complete(); ostream.Close(); WebResponse response = http.GetResponse(); Stream istream = response.GetResponseStream(); ParseHTML parse = new ParseHTML(istream); StringBuilder buffer = new StringBuilder(); List <String> list = new List <String>(); bool capture = false; Advance(parse, "table", 3); int ch; while ((ch = parse.Read()) != -1) { if (ch == 0) { HTMLTag tag = parse.Tag; if (String.Compare(tag.Name, "tr", true) == 0) { list.Clear(); capture = false; buffer.Length = 0; } else if (String.Compare(tag.Name, "/tr", true) == 0) { if (list.Count > 0) { ProcessTableRow(list); list.Clear(); } } else if (String.Compare(tag.Name, "td", true) == 0) { if (buffer.Length > 0) { list.Add(buffer.ToString()); } buffer.Length = 0; capture = true; } else if (String.Compare(tag.Name, "/td", true) == 0) { list.Add(buffer.ToString()); buffer.Length = 0; capture = false; } else if (String.Compare(tag.Name, "/table", true) == 0) { break; } } else { if (capture) { buffer.Append((char)ch); } } } }
private Uri BuildURL( TickerSymbol ticker, DateTime from, DateTime to ) { int selectedPair = -1; Uri url; #region Select Currency Pair switch (ticker.Symbol) { case "AUDJPY": selectedPair = DukascopyData["AUDJPY"]; break; case "AUDUSD": selectedPair = DukascopyData["AUDUSD"]; break; case "CADJPY": selectedPair = DukascopyData["CADJPY"]; break; case "CHFJPY": selectedPair = DukascopyData["CHFJPY"]; break; case "EURCHF": selectedPair = DukascopyData["EURCHF"]; break; case "EURGBP": selectedPair = DukascopyData["EURGBP"]; break; case "EURJPY": selectedPair = DukascopyData["EURJPY"]; break; case "EURUSD": selectedPair = DukascopyData["EURUSD"]; break; case "GBPEUR": selectedPair = DukascopyData["GBPEUR"]; break; case "GBPJPY": selectedPair = DukascopyData["GBPJPY"]; break; case "GBPUSD": selectedPair = DukascopyData["GBPUSD"]; break; case "JPYCHF": selectedPair = DukascopyData["JPYCHF"]; break; case "NZDUSD": selectedPair = DukascopyData["NZDUSD"]; break; case "XPDUSD": selectedPair = DukascopyData["XPDUSD"]; break; case "XPTUSD": selectedPair = DukascopyData["XPTUSD"]; break; case "USDCAD": selectedPair = DukascopyData["USDCAD"]; break; case "USDCHF": selectedPair = DukascopyData["USDCHF"]; break; case "USDJPY": selectedPair = DukascopyData["USDJPY"]; break; case "XAGUSD": selectedPair = DukascopyData["XAGUSD"]; break; case "XAUUSD": selectedPair = DukascopyData["XAUUSD"]; break; default: break; } #endregion Select Currency Pair if (selectedPair != -1) { /* * fromD = mm.dd.yyyy * np = 259, 1000, 1500, 2000 * interval = 60, 600, 3600, 1D, 7D, 1MO * DF = m/d/Y, m.d.Y, d.m.Y, m-d-Y, d-m-Y * endSym = win, unix * split = tz, coma, tab (; , tab) * */ // construck URL MemoryStream mstream = new MemoryStream(); FormUtility form = new FormUtility(mstream, null); form.Add("Stock", selectedPair.ToString()); form.Add("fromD", from.ToString("mm.DD.yyyy")); form.Add("np", "2000"); form.Add("interval", "1D"); form.Add("DF", "m-d-Y"); // date format form.Add("endSym", "win"); form.Add("split", "coma"); mstream.Close(); byte[] b = mstream.GetBuffer(); String str = "http://www.dukascopy.com/freeApplets/exp/exp.php?" + StringUtil.FromBytes(b); url = new Uri(str); } return(url); }
/// <summary> /// Use the session to search for the specified state or capital. The search /// method can be called multiple times per login. /// </summary> /// <param name="session">The session to use.</param> /// <param name="search">The search string to use.</param> /// <param name="type">What to search for(s=state,c=capital).</param> /// <returns>A list of states or capitals.</returns> public List <String> Search(String session, String search, String type) { String listType = "ul"; String listTypeEnd = "/ul"; StringBuilder buffer = new StringBuilder(); bool capture = false; List <String> result = new List <String>(); // Build the URL. MemoryStream mstream = new MemoryStream(); FormUtility form = new FormUtility(mstream, null); form.Add("search", search); form.Add("type", type); form.Add("action", "Search"); form.Complete(); Uri url = new Uri("http://www.httprecipes.com/1/8/menunc.php?session=" + session); WebRequest http = HttpWebRequest.Create(url); http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; Stream ostream = http.GetRequestStream(); // Perform the post. byte[] b = mstream.GetBuffer(); ostream.Write(b, 0, b.Length); ostream.Close(); // Read the results. WebResponse response = http.GetResponse(); Stream istream = response.GetResponseStream(); ParseHTML parse = new ParseHTML(istream); // Parse from the URL. Advance(parse, listType, 0); int ch; while ((ch = parse.Read()) != -1) { if (ch == 0) { HTMLTag tag = parse.Tag; if (String.Compare(tag.Name, "li", true) == 0) { if (buffer.Length > 0) { result.Add(buffer.ToString()); } buffer.Length = 0; capture = true; } else if (String.Compare(tag.Name, "/li", true) == 0) { result.Add(buffer.ToString()); buffer.Length = 0; capture = false; } else if (String.Compare(tag.Name, listTypeEnd, true) == 0) { result.Add(buffer.ToString()); break; } } else { if (capture) { buffer.Append((char)ch); } } } return(result); }
/** * Access the website and perform a search for either states or capitals. * @param search A search string. * @param type What to search for(s=state, c=capital) * @throws IOException Thrown if an IO exception occurs. */ public void Process(String search, String type) { String listType = "ul"; String listTypeEnd = "/ul"; StringBuilder buffer = new StringBuilder(); bool capture = false; // Build the URL and POST. Uri url = new Uri("http://www.httprecipes.com/1/7/post.php"); WebRequest http = HttpWebRequest.Create(url); http.Timeout = 30000; http.ContentType = "application/x-www-form-urlencoded"; http.Method = "POST"; Stream ostream = http.GetRequestStream(); FormUtility form = new FormUtility(ostream, null); form.Add("search", search); form.Add("type", type); form.Add("action", "Search"); form.Complete(); ostream.Close(); // read the results HttpWebResponse response = (HttpWebResponse)http.GetResponse(); Stream istream = response.GetResponseStream(); ParseHTML parse = new ParseHTML(istream); // parse from the URL Advance(parse, listType, 0); int ch; while ((ch = parse.Read()) != -1) { if (ch == 0) { HTMLTag tag = parse.Tag; if (String.Compare(tag.Name, "li", true) == 0) { if (buffer.Length > 0) { ProcessItem(buffer.ToString()); } buffer.Length = 0; capture = true; } else if (String.Compare(tag.Name, "/li", true) == 0) { ProcessItem(buffer.ToString()); buffer.Length = 0; capture = false; } else if (String.Compare(tag.Name, listTypeEnd, true) == 0) { ProcessItem(buffer.ToString()); break; } } else { if (capture) { buffer.Append((char)ch); } } } }