Spoofing the URL in a WebBrowser

2019-02-19 01:01发布

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?

3条回答
别忘想泡老子
2楼-- · 2019-02-19 01:47

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.

查看更多
贪生不怕死
3楼-- · 2019-02-19 02:02

If you try to change address bar URL, browser will navigate through that, and there's nothing you can do about that.

But, in your HTML, its possible to "fake" source URL for your resources, by using <BASE> tag.

I think this is closest you can go.

查看更多
贪生不怕死
4楼-- · 2019-02-19 02:06

You can inject a <BASE> element in your code and state that the base is http://example.com

查看更多
登录 后发表回答