Spoofing the URL in a WebBrowser

browser, c#, html, url

Solution

You could try and insert a `<base>` element in the head: http://www.w3.org/TR/html4/struct/links.html#edef-BASE

How to insert the tag is dependent upon the language you're using but you should aim to get the base tag directly after the `<head>` so that the resulting source reads:

<head><base href="http://example.com"/>

Of course, if there is already a `<base>` element in the document, you should remove that.

Problem

Let's say I get the source code of some page (e.g. http://example.com). I now want to write this source code to a WebBrowser, using something like: ``` myWebBrowser.Navigate("about:blank"); myWebBrowser.Document.Write(sourceCode); ``` Now, let's pretend that on the homepage of Example.com, there's a relative URL such as: ``` <img src="/logo.gif" /> ``` The WebBrowser will attempt to load it from `about:blank/logo.gif`. I want to tell the WebBrowser that the "current address" is `http://example.com` so that it uses `http://example.com/logo.gif` instead. Writing directly to the Url property of the WebBrowser will cause a Navigate(), which will get rid of any text I wrote. I am looking for a solution that works for other elements as well such as stylesheets, javascript (e.g. `<script language="text/javascript" src="myscript.js">`), links, etc., not just images. Is this possible?

Original source