Reputation: 29444
How do you create a method that does this:
Login to a website, then read a (member-only) page and return the HTML.
I came up with this (which obviously doesn't work because I don't know how to make it return the page content)
public string LoginAndReadPage() {
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate("hxxp://mysite.com/login");
}
private async void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (wb.Url.ToString().Contains("login"))
{
wb.Document.GetElementsByTagName("input").GetElementsByName("email")[0].SetAttribute("value", _login);
wb.Document.GetElementsByTagName("input").GetElementsByName("password")[0].SetAttribute("value", _password);
wb.Document.GetElementsByTagName("button")[0].InvokeMember("click");
}
else if (wb.Url.ToString().Contains("dashboard"))
{
return wb.DocumentText; // I want to return the content of mysite.com/dashboard
}
else
{
await Task.Delay(1000); //wait for 1 second just to let the WB catch up
wb.Navigate("hxxp://mysite.com/dashboard");
}
}
Thanks in advance
Upvotes: 0
Views: 40
Reputation: 786
What you're trying to do is called "scraping" or sometimes "webscraping". It's a big topic so I'd recommend googling it.
Potentially you could use something like Selenium via the C# Driver to do this as well. Selenium was designed for automated UI testing but it definitely has all the tools you need to do what you want.
Upvotes: 1