如果我使用Windows Form,则可以登录该网站,但是如果没有Windows Form类,则无法登录。
我的代码:
WebBrowser webBrowser = new WebBrowser();
webBrowser.Navigate("http://URL-Website");
webBrowser.ScriptErrorsSuppressed = true;
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
webBrowser.Document.InvokeScript("__doPostBack", new object[] { "ctl00$lbtDangnhap", "" });
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
HtmlDocument doc = webBrowser.Document;
HtmlElement user = doc.GetElementById("ctl00_ContentPlaceHolder1_ctl00_ctl00_User");
HtmlElement pass = doc.GetElementById("ctl00_ContentPlaceHolder1_ctl00_ctl00_Pass");
user.SetAttribute("value", "usernameLogin");
pass.SetAttribute("value", "passwordLogin");
HtmlElement submit = doc.GetElementById("ctl00_ContentPlaceHolder1_ctl00_ctl00_btnSubmit");
submit.InvokeMember("click");
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
我得到异常HtmlDocument用户== null(如果我不使用Windows窗体)
System.NullReferenceException:'对象引用未设置为对象的实例。'
请帮助我理解我的问题及其解决方法。