ASP.net C#web-browser控件总是只打开浏览器?

时间:2015-02-18 13:42:33

标签: c# asp.net internet-explorer firefox webbrowser-control

在Asp.net C#中,使用网络浏览器控件,我需要实时自动登录在实时网站中,它仅适用于Internet Explorer(IE),并且无法在中使用Firefox和Chrome以及其他浏览器。如何解决这个问题

这是我的C#代码

void capture()
    {


            Thread thread = new Thread(delegate()
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ScrollBarsEnabled = false;
                    browser.AllowNavigation = true;
                    browser.Navigate("My URL");
                    browser.Width = 1024;
                    browser.ScriptErrorsSuppressed = true;
                    browser.Height = 768;
                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

    }


    private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser1 = sender as WebBrowser;

        HtmlDocument doc = browser1.Document;

        string user = txtUsername.Text.Trim();
        string pass = password;

        doc.GetElementById("Your userID").SetAttribute("value", user);
        doc.GetElementById("Your Password").SetAttribute("value", pass);

        //get Button on page and fire its click event
        doc.GetElementById("Your Buttonid").InvokeMember("Click");

        Thread thread = new Thread(delegate()
        {
            using (WebBrowser browser = new WebBrowser())
            {
                browser.ScrollBarsEnabled = false;
                browser.AllowNavigation = true;
                browser.Navigate("My URL");
                browser.Width = 1024;
                browser.Height = 768;

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted1);
                while (browser.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();

    } 

1 个答案:

答案 0 :(得分:1)

好吧,你正在使用围绕IE引擎构建的WebBrowser控件。您将不得不使用其他东西来获取另一个浏览器。查看Awesomium.

之类的内容