Question

How do I set the url of an htmldocument after I've written to it. For example:

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

If I do myDoc.url = "http://www.google.com" it attempts to load google.
How do I set the url without having it attempt to load that url?

Was it helpful?

Solution

These steps should give you a document with correct URL and your own content:

  1. Create document directly from URL (so you don't have to set the URL later)
  2. Stop document download (because you don't need the content)
  3. Fill document with your content

This code shows how to do it:

// 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
// ... 

Note: It's hard to guess how much content can be downloaded between steps 1 and 2 because loading happens asynchronously. So it's probably good to check if the document is indeed empty before doing step 3. If not, clear the document and proceed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top