简体   繁体   English

C#/。NET Webclient,等待页面完成加载

[英]C#/.NET Webclient, wait for page to finish loading

I'm trying to retrieve the html of a page with some Ajax on. 我正在尝试检索带有一些Ajax的页面的html。

Problem is that Webclient.Downloadstring() returns to fast, so the Ajax page haven't finished loading => I'm not getting the right html :( 问题是Webclient.Downloadstring()返回速度很快,因此Ajax页面尚未完成加载=>我没有得到正确的html :(

Is it possible to call another function or similar, so I for example request the page, wait a few seconds and then read the response? 是否可以调用另一个函数或类似的函数,所以例如请求页面,等待几秒钟,然后读取响应? (so I allow the Ajax to finish loading before I retrieve the html) (所以我允许Ajax在检索html之前完成加载)

Thanks, Louisa 谢谢路易斯

The WebClient by default only fetches the (HTML) contents of a single URL. 默认情况下, WebClient仅获取单个 URL的(HTML)内容。 It does not parse HTML and thus does not know about any CSS, images or javascript used on the page. 解析HTML,因此不知道在页面上使用的任何CSS,图片或JavaScript。 You are trying to emulate the functionality of a full-blown browser, for which the WebClient alone is insufficient. 您正在尝试模仿功能全面的浏览器的功能,而仅WebClient不足以实现此功能。

To achieve your desired behaviour, you will have to not only retrieve the HTML, but then also parse it, retrieve and execute javascript on the page and then get the resulting DOM. 为了实现所需的行为,您不仅必须检索HTML,还必须对其进行解析,检索和执行页面上的javascript,然后获取结果DOM。 This is most easily achieved through a library that provides the functionality of a webbrowser to your application. 通过可为您的应用程序提供Web浏览器功能的库,最容易实现这一点。 Examples include System.Windows.Forms.WebBrowser (WinForms), System.Windows.Controls.WebBrowser (WPF) or Awesomium . 示例包括System.Windows.Forms.WebBrowser (WinForms), System.Windows.Controls.WebBrowser (WPF)或Awesomium

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM