我使用Winform WebBrowser加载网站。该网站包含一个iframe。这里的URL: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe
当我使用Navigate()
方法加载该网址时。 WebBrowser显示texteditor和iframe。
我想通过我的应用程序定义的代理加载网站,然后将该数据注入Web浏览器控件。
当我尝试使用HttpWebRequest
加载网站时,如下所示:
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe");
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
webBrowser1.DocumentStream = myResponse.GetResponseStream();
WebBrowser也显示html的texteditor,但不显示iframe。它只显示iframe的URL。
为什么WebBrowser不显示iframe内容?虽然HTML,CSS,JS仍然有用,但iframe不是吗?
我使用HttpWebRequest而不是Navigate(),因为我想使用许多代理来加载网页。 !
答案 0 :(得分:1)
这是因为您正在将HTML流引入浏览器。这就像做一个AJAX调用,然后使用javascript' document.write
将内容写入文档。
问题在于文档网址实际上是about:blank
,因此任何加载了具有iframe相对网址的网页都将是about://domain.com/path/to/iframe-page.html
而不是http://domain.com/path/to/iframe-page.html
修改强>
因此,如果您要加载的页面是由您定义的,那么只需使用绝对URL。否则它不会工作。