get current webbrowser url or webbrowser content

▼魔方 西西 提交于 2019-12-25 05:01:32

问题


I made a login button and want to check if the user logged in or not. I though 2 ways to do that.

  1. 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)
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!