更改 IE 用户代理
我正在使用 WatiN 来自动化 Internet Explorer,到目前为止,效果非常好。但是,我真的希望能够更改 IE 的用户代理,以便服务器认为它实际上是 Firefox 或其他浏览器。
Firefox 用户代理字符串如下所示:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
使用以下代码
RegistryKey ieKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent");
ieKey.SetValue("", "Mozilla/5.0");
ieKey.SetValue("Compatible", "Windows");
ieKey.SetValue("Version", "U");
ieKey.SetValue("Platform", "Windows NT 5.1; en-US");
ieKey.DeleteSubKeyTree("Post Platform");
我已经能够更改 IE 的用户代理
从Mozilla/4.0(兼容;MSIE 8.0;Windows NT 6.1;Trident/4.0;AskTbMP3R7/5.9.1.14019)
到
Mozilla/4.0(Windows;U;Windows NT 6.1;Trident/4.0;en-US;rv:1.9) 字符串.2.13)
现在的问题是:如何删除 Trident/4.0 部分并在括号后添加“Gecko/20101203 Firefox/3.6.13”部分?
我真的很想以编程方式执行此操作在 C# 中,不使用任何 IE 插件。
提前致谢。
I'm using WatiN to automate Internet Explorer, and so far it's been great. However, I would really like to be able to change the user agent of IE so the server thinks it's actually Firefox or some other browser.
A Firefox useragent string look something like:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
With the following code
RegistryKey ieKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent");
ieKey.SetValue("", "Mozilla/5.0");
ieKey.SetValue("Compatible", "Windows");
ieKey.SetValue("Version", "U");
ieKey.SetValue("Platform", "Windows NT 5.1; en-US");
ieKey.DeleteSubKeyTree("Post Platform");
I have been able to change the IE useragent string from
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; AskTbMP3R7/5.9.1.14019)
to
Mozilla/4.0 (Windows; U; Windows NT 6.1; Trident/4.0; en-US; rv:1.9.2.13)
Now, the question: how do I delete the Trident/4.0 part and add the "Gecko/20101203 Firefox/3.6.13" part after the parentheses?
I would really like to do this programatically in C#, without using any IE add-ons.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非 WebBrowser 控件在进程内运行,否则不支持在没有任何 IE 插件的情况下在 C# 中执行此操作的方法,在这种情况下,您可以使用
UrlMkSetSessionOption()
API。请参阅 用户代理字符串:使用和滥用如果您愿意使用
附加组件
,请参阅http://www.enhanceie.com/ietoys/uapick.asp现在,有一个不受支持的黑客可以做到这一点,我不会不建议 - 也就是说,您可以将
COMPATIBLE
字符串替换为Firefox UA
的其余部分,后跟CRLF
和文本 <代码>忽略:。这将导致 HTTP 标头“包装”到新标头中,因此您将发送如下所示的标头:There's no supported way to
do this in C# without any IE Addons
unless the WebBrowser control is running in-proc, in which case you can use theUrlMkSetSessionOption()
API. See The User-Agent String: Use and AbuseIf you are willing to use
add-ons
, see http://www.enhanceie.com/ietoys/uapick.aspNow, there's an unsupported hack to do this that I wouldn't recommend-- namely, you could replace the
COMPATIBLE
string with the remainder of theFirefox UA
, followed by aCRLF
and the textIGNORE:
. This would cause the HTTP header to "wrap" into a new header, so you'd be sending Headers that look something like:我无法想象你为什么要这样做。 其他浏览器需要冒充 IE 的情况并不罕见,因为那些脑残网站仍然坚持阻止其他任何内容,但我从未听说过有人想要反过来做。至少在过去十年左右的时间里不是这样。
不管怎样,正如您已经发现的,使用 IE 并不像使用其他浏览器那么容易。
如果您确实想这样做,我的建议是通过代理,并让它为您发送欺骗性的用户代理。用任何语言编写都应该相对容易。您可以在浏览器所在的本地计算机上运行代理,这对浏览器用户和网站都是透明的(在本地计算机上,甚至 IP 地址都是正确的,这可能是一个问题对于远程代理)
就其价值而言,一些防病毒/反恶意软件应用程序实际上已经这样做了,可以选择阻止用户代理和引用数据以防止网站跟踪它们。据我所知,他们几乎完全使用了这种技术(这些相同的本地代理也用于在恶意软件到达浏览器之前将其过滤掉)。
I can't imagine why you'd want to do this. It's not uncommon for other browsers to need to pretend to be IE due to brain-dead sites that still insist on blocking anything else, but I've never heard of anyone wanting to do it the other way round. At least not in the last ten years or so.
Anyway, as you've already found, it's not as easy to do with IE as it is with other browsers.
If you really want to do this, my suggestion would be to go through a proxy, and have that send the spoofed user agent for you. That ought to be relatively easy to write in any language. You can run the proxy on the local machine where the browser is, and it'll all be transparent to both the browser user and the web site (being on the local machine, even the IP address will be correct, which can be a problem for remote proxies)
For what it's worth, some anti-virus/anti-malware apps actually do this already, having options to block the user agent and referral data to prevent sites from tracking them. As far as I know, they use pretty much exactly this tecnique (those same local proxies are also used to filter out the malware before it gets to the browser).