在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();
}