使用HttpWebRequest时不显示WebBrowser iframe(使用自己的代理导航)

时间:2015-04-25 15:42:12

标签: c# html winforms iframe

我使用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。 enter image description here 为什么WebBrowser不显示iframe内容?虽然HTML,CSS,JS仍然有用,但iframe不是吗?

我使用HttpWebRequest而不是Navigate(),因为我想使用许多代理来加载网页。 !

1 个答案:

答案 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。否则它不会工作。