HTTPWebRequest等待Ajax加载内容 [英] HTTPWebRequest waits for content to load by Ajax

查看:106
本文介绍了HTTPWebRequest等待Ajax加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用httpWebRequest来获取完整的网页,但是我得到的响应不是完整的网页,因为网页的一部分是由AJAX加载的,而这部分需要一段时间(通常为10到30秒)加载).有没有一种方法可以设置/强制httpWebRequest等待几秒钟,然后才能检索网页的内容?

I am trying to use httpWebRequest to get a complete web page, but the response that I get is not a complete web page because part of the web page is loaded by AJAX and this part takes a while (usually 10 - 30 seconds to load). Is there a way that I can set/force httpWebRequest to wait for a number of seconds before retrieving the content of a web page?

任何帮助将不胜感激!

谢谢

推荐答案

如果该站点使用AJAX加载数据,则使用HttpWebRequest可能不起作用.原因是该站点可能正在使用document.onLoad()或page.onLoad()方法向远程站点发出GET请求.这发生在浏览器中的javascript执行内部.

If the site is using AJAX to load data, then using HttpWebRequest might not work. The reason is that the site is probably using the document.onLoad() or page.onLoad() method to issue a GET request to the remote site. And this happens inside the javascript execution in the browser.

如果要使其正常工作,您有两种选择.

If you want to get this to work, you have two options.

1)发出对容器页面(您访问的主页面)的请求,并向容器页面正在使用AJAX加载的包含页面发出第二个请求.为了找到包含的页面,您将不得不使用带有firebug插件的Firefox来找出内部页面的URL.

1) Issue the request for the container page (the main page that you access), and a second request to the contained page that is being loaded by the container page using AJAX. In order to find the contained page, you will have to use Firefox with the firebug plugin to figure out the URL of the inner page.

2)使用一些支持Javascript和HTML/DOM的更高级别的框架.例如,您可以尝试使用托管在.NET中的Microsoft的WebBrowser控件.或者,您可以使用其他框架,只要该框架支持javascript并理解HTML.

2) Use some higher level frameworks that support Javascript and HTML/DOM. For eg, you could try using the WebBrowser control from microsoft, hosted in .NET. Or you could use other frameworks, provided the framework supports javascript and understands HTML.

这篇关于HTTPWebRequest等待Ajax加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆