如何在IHTMLDocument2中设置url,而不需要导航到新页面



我如何设置一个htmldocument的url后,我写了它。例如:

WebBrowser wb = new WebBrowser();
wb.Navigate(new Uri(location, UriKind.Absolute));
IHTMLDocument2 myDoc = new HTMLDocumentClass();
myDoc.write(new object[] { wb.DocumentText});
myDoc.close();

如果我执行myDoc.url = "http://www.google.com",它会尝试加载google。
如何设置url而不尝试加载该url?

这些步骤应该会给你一个具有正确URL和你自己的内容的文档:

  1. 直接从URL创建文档(这样你就不必设置URL)
  2. 停止文件下载(因为你不需要内容)
  3. 用你的内容填充文档

下面的代码展示了如何做到这一点:

// 1. Create new document from URL
IHTMLDocument2 NewDoc = (wb.Document as IHTMLDocument4).createDocumentFromUrl("http://www.stackoverflow.com", "null");
// 2. Immediately stop navigating; the URL is still set
NewDoc.execCommand("Stop", false, null);
// 3. Now write your stuff to the document
// ... 

注意:很难猜测在步骤1和步骤2之间可以下载多少内容,因为加载是异步进行的。因此,最好在执行第3步之前检查文档是否确实为空。

最新更新