编程语言
首页 > 编程语言> > javascript-Internet Explorer用户代理标头在HTTP调用与navigator.userAgent中有所不同

javascript-Internet Explorer用户代理标头在HTTP调用与navigator.userAgent中有所不同

作者:互联网

我需要获取AngularJS用于发送HTTP请求的User-Agent标头.当我使用Internet Explorer时,在Fiddler中,我看到在请求中发送的User-Agent等于:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

但是,当我使用navigator.userAgent获取用户代理时,它将返回以下内容:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko

我添加了< meta http-equiv =“ X-UA-Compatible” content =“ IE = edge” />标记在HTML< head>中,希望可以解决该问题,但不能解决.我还尝试了启用和禁用Intranet(在本例中为localhost)站点的兼容性视图.

背景:用户代理正通过查询字符串(以及查询字符串中的auth令牌)传递给Windows单击一次应用程序. Windows应用程序需要使用与AngularJS应用程序相同的身份验证令牌和用户代理,否则,如果用户代理不同,则HTTP API调用将导致401. Windows应用不应要求用户登录,而应使用与浏览器用户相同的身份验证上下文.

如何获取在Fiddler中观察到的实际User-Agent发送?

解决方法:

据我所知,在Internet Explorer版本9及更高版本中,在HTTP请求发送的版本和通过navigator.userAgent获得的版本之间的用户代理字符串几乎将有所不同,因为该字符串中存在所谓的功能标记(仅偶然地进行全新安装时,它们可能会短暂停留一会儿).这里解释了其背景:http://msdn.microsoft.com/en-us/library/ms537503%28v=vs.85%29.aspx

从msdn文档:

Earlier versions of Internet Explorer included feature tokens defined using the Pre-Platform and
Post-Platform keys part of the user-agent string during the HTTP negotiation process. Over time,
this lead to overly long user-agent strings, which in turn created problems for certain web servers.
Problems usually appeared when user-agent strings were longer than 256 characters. As of Internet
Explorer 9, the user-agent string no longer includes feature tokens during HTTP negotiation. Feature
tokens are included in the value returned by the userAgent property of the navigator object.
Applications that rely on the earlier behavior should be modified accordingly.

使用< meta http-equiv =“ X-UA-Compatible” content =“ IE = EmulateIE8” />设置兼容性查看模式.元标记也不会影响用户代理问题(我已经在MSIE 10和MSIE 11中对其进行了测试).

我可以想象的唯一的局部解决方案(不可靠)是从通过Java脚本获得的字符串中剥离已知特征属性,然后尝试比较这些字符串.

不幸的是,我怀疑您应该尝试为不涉及用户代理字符串比较的问题找到其他解决方案.

标签:angularjs,user-agent,http-headers,internet-explorer,javascript
来源: https://codeday.me/bug/20191121/2051809.html