问题
I made a login button and want to check if the user logged in or not. I though 2 ways to do that.
- Check if the webbrowser url is equal with a url (If user login successfully the url is url1, and if not the url is url2)
- Check if the current webbrowser content contains a word (for example if user login successfully, then in the redirected page should have "Thank you" message)
I want to mention that all is working my only problem is to check if he logged in or not
public void Login()
{
webBrowser1.Document.GetElementById("username").SetAttribute("value", txtUsername.Text);
webBrowser1.Document.GetElementById("password").SetAttribute("value", txtPassword.Text);
foreach (HtmlElement item in webBrowser1.Document.GetElementsByTagName("input"))
{
if (item.OuterHtml.Contains("input_submit"))
{
item.InvokeMember("Click");
break; // Here the webbrowser redirect right?
}
}
if (webBrowser1.Document.Body.InnerText.Contains("Thank you"))
con = true;
else
con = false;
}
回答1:
I suggest you use HttpRequest to emulate the login progress,use HttpResponse to get webserver response and content and use global CookieContainer to store the cookies.
When you get login successful message,you can use other HttpRequest with globval CookieContainer to get any web pages you want download or access.
i hope this may help you.
you can use Httpwatch(www.httpwatch.com) to watch how Internet Explorer exchange data with WebServer.you can also use IE9 devloper tools to do this(press F12 in IE9).
来源:https://stackoverflow.com/questions/11250669/get-current-webbrowser-url-or-webbrowser-content