为什么 HttpWebRequest 和 WebBrowser 获得不同的 HTML 源代码?
我正在尝试从网页获取源代码。 WebBrowser 控件为我提供了我正在寻找的信息。但是,我想使用 HttpWebRequest,但它提供的源代码与 WebBrowser DocumentText 不同。
谁能告诉我如何使用 HttpWebRequest 获得与 WebBrowser 相同的源代码?
网页浏览器代码:
WebBrowser1.Navigate("http://www.networksolutions.com/whois/results.jsp?domain=" & txtUrl.Text)
textbox1.Text = WebBrowser1.DocumentText
网页浏览器结果:
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 结果:
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:
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有些网站会查看用户代理字符串或其他因素,并返回基于此而变化的内容。我编写了许多下载网页的项目,并且遇到过几次这种情况。
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.
这是一个老问题,但发生这种情况的原因是 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.