为什么 HttpWebRequest 和 WebBrowser 获得不同的 HTML 源代码?

发布于 2024-10-09 21:45:51 字数 1030 浏览 3 评论 0原文

我正在尝试从网页获取源代码。 WebBrowser 控件为我提供了我正在寻找的信息。但是,我想使用 HttpWebRequest,但它提供的源代码与 WebBrowser DocumentText 不同。

谁能告诉我如何使用 HttpWebRequest 获得与 WebBrowser 相同的源代码?

网页浏览器代码:

WebBrowser1.Navigate("http://www.networksolutions.com/whois/results.jsp?domain=" & txtUrl.Text)
textbox1.Text = WebBrowser1.DocumentText

网页浏览器结果:

http://textbin.com/f4368

HttpWebRequest 代码:

Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
request.KeepAlive = False
request.Timeout = 10000

Dim response As System.Net.HttpWebResponse = request.GetResponse()

Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd()

HttpWebRequest 结果:

http://textbin .com/2h445

I am trying to get the source code from a webpage. The WebBrowser control is giving me the information that I am looking for. However, I want to use HttpWebRequest, but its giving me different source code than the WebBrowser DocumentText.

Can anyone please tell me how can I get the same source code as WebBrowser using HttpWebRequest?

WebBrowser Code:

WebBrowser1.Navigate("http://www.networksolutions.com/whois/results.jsp?domain=" & txtUrl.Text)
textbox1.Text = WebBrowser1.DocumentText

WebBrowser Result:

http://textbin.com/f4368

HttpWebRequest Code:

Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(url)
request.KeepAlive = False
request.Timeout = 10000

Dim response As System.Net.HttpWebResponse = request.GetResponse()

Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd()

HttpWebRequest Result:

http://textbin.com/2h445

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-10-16 21:45:51

有些网站会查看用户代理字符串或其他因素,并返回基于此而变化的内容。我编写了许多下载网页的项目,并且遇到过几次这种情况。

Some sites will look at the user-agent string or other factors and return content that varies based on this. I've written a number of projects that downloaded web pages and have run into this a few times.

滥情稳全场 2024-10-16 21:45:51

这是一个老问题,但发生这种情况的原因是 MSHTML(Windows html 渲染引擎)在渲染之前修改传入的 HTML。您可以更改 .NET Web 浏览器的渲染模式以使用 IE7、8 或 9 渲染引擎中的任何一个,您会发现它们从浏览器返回的 HTML 存在巨大差异 - IE9 将是最相似的到你在 HttpWebRequest 中看到的内容。

This is an old-ish question but the reason this happens is that MSHTML - the Windows html rendering engine - modifies the incoming HTML before it renders it. You can change the rendering mode of the .NET web browser to use any of IE7, 8, or 9 rendering engines and you'll see HUGE differences in the HTML they return back out of the browser - IE9's is going to be the most similar to what you see come down in HttpWebRequest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文