更改 IE 用户代理 [英] Change IE user agent

查看:33
本文介绍了更改 IE 用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WatiN 来自动化 Internet Explorer,到目前为止它已经很棒了.但是,我真的希望能够更改 IE 的用户代理,以便服务器认为它实际上是 Firefox 或其他浏览器.

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.

Firefox 用户代理字符串类似于:

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

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");

我已经能够从

Mozilla/4.0(兼容;MSIE 8.0;Windows NT 6.1;Trident/4.0;AskTbMP3R7/5.9.1.14019)

Mozilla/4.0 (compatible; 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)

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 would really like to do this programatically in C#, without using any IE add-ons.

提前致谢.

推荐答案

除非 WebBrowser 控件正在进程内运行,否则没有支持的方法来在没有任何 IE 插件的 C# 中执行此操作,在这种情况下您可以使用 UrlMkSetSessionOption() API.请参阅 用户代理字符串:使用和滥用

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 the UrlMkSetSessionOption() API. See The User-Agent String: Use and Abuse

如果您愿意使用附加组件,请参阅http://www.enhanceie.com/ietoys/uapick.asp

If you are willing to use add-ons, see http://www.enhanceie.com/ietoys/uapick.asp

现在,有一个不受支持的 hack 可以做到这一点,我不推荐——也就是说,您可以用 的其余部分替换 COMPATIBLE 字符串Firefox UA,后跟 CRLF 和文本 IGNORE:.这会导致 HTTP 标头包装"到一个新标头中,因此您将发送类似于以下内容的标头:

Now, there's an unsupported hack to do this that I wouldn't recommend-- namely, you could replace the COMPATIBLE string with the remainder of the Firefox UA, followed by a CRLF and the text IGNORE:. This would cause the HTTP header to "wrap" into a new header, so you'd be sending Headers that 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忽略:MSIE 8.0;视窗 NT 6.1;三叉戟/4.0;AskTbMP3R7/5.9.1.14019)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 IGNORE: MSIE 8.0; Windows NT 6.1; Trident/4.0; AskTbMP3R7/5.9.1.14019)

这篇关于更改 IE 用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆