- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 c# 的新手,我正在尝试构建一个登录网站并返回其源代码的程序。问题是,我为页面加载时注册了一个事件监听器,但是当我调试它时,它在设置相同事件后退出,实际上并没有在页面“加载”后执行我希望它执行的操作。
这是来源-
using System;
using System.Windows.Forms;
namespace WIN
{
class Program
{
string url = -snip-;
string username = -snip-;
string password = -snip-;
string task = -snip-;
string action = -snip-;
string timezone = -snip-;
private void Login()
{
Console.WriteLine("Started.");
Console.ReadLine();
Console.WriteLine("Declaring WebBrowser instance browser...");
WebBrowser browser = new WebBrowser();
Console.WriteLine("Done.");
Console.ReadLine();
Console.WriteLine("Registering an event for when the page finishes loading...");
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded);
Console.WriteLine("Done.");
Console.ReadLine();
Console.WriteLine("Using method Navigate of browser instance with url parameter...");
browser.Navigate(url);
Console.WriteLine("Done.");
Console.ReadLine();
}
private void pageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine("Declaring WebBrowser instance browser as sender...");
WebBrowser browser = sender as WebBrowser;
Console.WriteLine("Done.");
Console.ReadLine();
string response = browser.DocumentText;
Console.WriteLine("Searching for authenticity token...");
// looks in the page source to find the authenticity token.
// could also use regular expressions here.
int index = response.IndexOf("authenticity_token");
int startIndex = index + 41;
string authenticityToken = response.Substring(startIndex, 40);
Console.WriteLine("Found authenticity token.");
Console.WriteLine("Unregistering first event handler...");
// unregisters the first event handler
// adds a second event handler
browser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(pageLoaded);
Console.WriteLine("Done.");
Console.WriteLine("Adding second event handler...");
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded2);
Console.WriteLine("Done.");
Console.Read();
Console.WriteLine("Formatting data to be posted to server...");
string postData = string.Format("_user={0}&_pass={1}&authenticity_token={2}&_task{3}&_action{4}&_timezone{5}", username, password, authenticityToken, task, action, timezone);
Console.WriteLine("Done.");
Console.Read();
Console.WriteLine("Declaring ASCIIEncoding instance enc...");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Console.WriteLine("Done.");
Console.Read();
// we are encoding the postData to a byte array
Console.WriteLine("Encoding postData to a byte array...");
browser.Navigate(url, "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
Console.WriteLine("Done..");
Console.Read();
}
[STAThread]
static void Main(string[] args)
{
Program p = new Program();
p.Login();
}
}
}
从所有这些控制台输出中,它只会到达使用方法 Navigate of browser instance with url parameter...
最佳答案
WebBrowser 要求您的程序提供一个消息循环。否则它不会触发事件。这通常是任何使用单线程 COM 组件的程序的要求。或者用更易于理解的术语来说:您不能让程序忙于从控制台读取数据,同时触发 DocumentCompleted 等事件。一个线程一次只能做一件事。您可以通过编写一个 Winforms 应用程序或使用 Application.Run() 自己启动一个来泵送消息循环。使用消息循环,一个线程可以一次做不止一件事。但这确实需要与您现在编写的代码截然不同的代码,您仍然不能使用 Console.ReadLine(),而是使用 TextBox。
您可以通过在单独的线程中运行浏览器来挽救您所拥有的,您将在 this answer 中找到您需要的代码.
关于c# - WebBrowser 没有到达 DocumentCompleted 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10720703/
运行这一行: webbrowser.open(page.path, new=0, autoraise=True) 我总是在新标签页中打开页面。 我怎样才能将它放在与它应该使用 new=0 相同的选项卡
基于 MSDN 上的这篇文章:How To Determine When a Page Is Done Loading in WebBrowser Control ,并且从过去关于 StackOver
我不明白为什么这行简单的代码行不通: // Bulding tree var declaration = new XDeclaration("1.0", "UTF-8", "yes"); var ro
这些是等价的吗? WebBrowser.Navigate('http://google.com'); 和 WebBrowser.Stop(); WebBrowser.Navigate('http://
我需要获取有关 Webbrowser 的滚动条(位置、大小、可见性)的信息控制外部应用程序,我尝试使用 GetScrollBarInfo来 self 之前的功能 question ,但该函数总是返回
目前在带有 WebBrowser 的 vb6 项目中,我遇到了一个问题。我想在我的应用程序中显示 adsense 广告,所以我创建了一个 html 页面并将文件放入服务器(adsense 批准),我尝
Windows Phone 8 应用程序,WebBrowser 控件。我通过 NavigateToString 加载了一大块 HTML (设置后 IsScriptEnabled=true )。一段时间
我有一个嵌入 WebBrowser 的 native Windows 应用程序, IE。 CLSID_WebBrowser 8856F961-340A-11D0-A96B-00C04FD705A2 S
提前致谢。 在我的应用程序中,我将 WebBrowser 控件替换为 Microsoft 的 WebView2 . 我想知道 WebView2 中 WebBrowser 控件的以下成员的等价物。 We
我怎样才能让 Web 浏览器调用自定义方案来启动 Activity,然后我想按下后退按钮但不返回 Web 浏览器。 我只想在调用网络浏览器然后调用我的方案启动另一个 Activity 时实现转发。当我
我们的目标是:嵌入在 .net winform 中的支持 Watin 的浏览器测试。 目前,我们正在使用 .net WebBrowser 控件将浏览器行为嵌入到 winform 中。我们使用如下代码将
我正在使用将 WebBrowser 控件嵌入到 Windows 窗体中的应用程序,我注意到一个严重的内存泄漏问题。在阅读了互联网上的其他类似案例后,问题似乎出在 IE 上。 我问过其他人是如何解决这个
我使用了“LoadCompleted”事件,基本上一旦收到该事件,我就需要截取屏幕截图(我已经构建)。问题是一旦这个事件结束,屏幕截图就只是空白(页面还没有加载),只有当我延迟 5-10 秒时,屏幕截
我试图自动打开多个用户配置文件,给定几个不同网站上的名称列表,但我找不到在新窗口中打开链接的方法,这意味着我无法对我打开的不同网站进行排序自己的橱窗收藏。 这是我的代码: import webbrow
我使用 vb.net + wpf+ System.Windows.Forms.WebBrowser 在 wpf 表单上显示网页内容。 此网站“http://manuim.com/laisha/”在控件
有没有办法检查用户是否安装了 PDF 阅读器? 也许使用 ActiveX 组件? 请你帮助我好吗? 最佳答案 不,我不这么认为 - 但默认情况下你总是可以通过谷歌的 PDF 阅读器直接链接你的链接 -
尝试在 WPF(C#/XAML、.NET 4.0)WebBrowser 应用程序中测试基本浏览器概念。到目前为止,唯一的问题是以编程方式缩放。有没有人有这方面的经验? MSDN 没有列出任何内容:ht
首先是一些定义,以保持清楚。 User: A live person, using the software Client: A company that is paying for a custom
尝试使用 webBrowser.Navigate("https://home.nest.com/") 加载页面时,我不断收到脚本错误.它会从我的普通互联网浏览器中正常显示,但不会在我的程序中显示。 谁
显然,WPF WebBrowser control 存在一些严重的键盘和焦点问题。 .我已经组装了一个简单的 WPF 应用程序,只有一个 WebBrowser 和两个按钮。该应用程序加载一个非常基本的
我是一名优秀的程序员,十分优秀!