Now it is time to get history data from Internet. There is new button "Get" on the right of URL text box named getHistory. Google's stock is chosen and after click on right Get button we have file named P:\Private\Analizator\GOOG-US.txt in our working directory.
private void getHistory_Click(object sender, EventArgs e)
{
WebRequest req = WebRequest.Create(urlHistory.Text); // to use WebRequest we need to add "using System.Net;" row
req.Proxy = null; // i do not use proxy
string selectedIndices = listIndices.SelectedItem.ToString(); // get selected indice
selectedIndices=selectedIndices.Replace(":", "-"); // file name can not contain ":"
selectedIndices = workingPath + selectedIndices + ".txt"; // make full path,file name and add .txt extension as it is text file
using (WebResponse res = req.GetResponse()) // get response from internet
using (Stream s = res.GetResponseStream()) // make a stream
using (StreamReader sr = new StreamReader(s)) File.WriteAllText(selectedIndices, sr.ReadToEnd()); // save stream to file
// System.Diagnostics.Process.Start(selectedIndices); // remove comment and result can be seen in text editor
}
No comments:
Post a Comment