Windows 服务 Webbrowser 对象无效转换异常错误

Sam*_*sey 5 .net c# browser

我在使用 Windows 服务 webbrowser 对象时遇到了一些麻烦。它试图将用户名和密码的值加载到站点,但一直失败并抛出以下错误:

System.InvalidCastException: Specified cast is not valid. at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation() at System.Windows.Forms.WebBrowser.get_Document() at MyWindowsService.MyDataProcessor.login()

我用来进行此调用的代码是:

MyWebBrowser.Document.All["Login"].SetAttribute("Value", username);
            MyWebBrowser.Document.All["Password"].SetAttribute("Value", password);
            MyWebBrowser.Document.All["submit"].InvokeMember("Click");
Run Code Online (Sandbox Code Playgroud)

关于为什么它一直失败的任何想法?在此先感谢您的帮助。

Axl*_*Axl 1

我不确定这是否可以解决问题,但您可以检查当前对象或 WebBrowser.InvokeRequired 上的 InvokeRequired 属性,并使用 MethodInvoker 之类的东西来调用您的函数或辅助函数来访问 WebBrowser.Document。

http://www.megasolutions.net/cSharp/(WebBrowser_Document-==-null)-throws-InvalidCastException-43126.aspx