I have an input file saved in a specified folder and used a stream reader to read the content one line at a time. So my program reads the first line and adds the content in the textbox. I wanted to get the first line of the text file, and use it as an input for the program via textbox. So my question would be how do I read a textbox content that was displayed from an input file and use it as an input for the program.
string inputFile = @"c:\temp\ScrappedData\input.txt";
Numbers(inputFile);
//gets the contents of the input file loaded by Numbers method
string lakeName = textBox1.Text;
string partialWebAdd = "http://www.google.com?=";
//Gets the url entered by the user from the textbox
string url = partialWebAdd + lakeName;
MessageBox.Show(url.ToString());
//string url = textBox1.Text;
So, Numbers(inputFile) loads the first line from input.txt(which is some numerical value that matches data in the website) and the website address displays good. but when I actually try to scrape it, webclient is not reading the input of the textbox. Do you know why? I tried using same code without getting input from the text file, rather typed whole website data into the box and it works fine. Thank you for your ideas. Below is the part of code for Number Method and webrequest.
string sourceCode = GetSource.getSourceCode(url);
//Marks the start point of scrape
int startIndex = sourceCode.IndexOf("Name:");
//Marks the endpoint of the html to scrape
int endIndex = sourceCode.IndexOf("For more information");
//Gets the string between the specified startIndex and endIndex
MessageBox.Show(startIndex.ToString()+ "dang bro" +endIndex.ToString());
sourceCode = sourceCode.Substring(startIndex, endIndex - startIndex);
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//Write the contents of the webpage to the file specified in path #162
StreamWriter sWriter = new StreamWriter(path);
sWriter.Write(sourceCode);
MessageBox.Show("Contents have been Scrapped!");
textBox1.Clear();
sWriter.Close();
Webrequest class:
class GetSource
{
public static string getSourceCode(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
string sourceCode = streamReader.ReadToEnd();
streamReader.Close();
response.Close();
return sourceCode;
}
}
}
Number Method:
public void Numbers(string filename)
{
string input;
if (System.IO.File.Exists(filename) == true)
{
System.IO.StreamReader objectReader;
objectReader = new System.IO.StreamReader(filename);
while ((input = objectReader.ReadLine()) != null)
{
Single output;
if (Single.TryParse(input, out output))
{
textBox1.AppendText(output.ToString());
MessageBox.Show("input to the textbox from input.txt was successful");
}
else
{
}
}
objectReader.Close();
}
else
{
MessageBox.Show("No Such File" + filename);
}
}