我可以修改 Request.Headers 集合吗? [英] Can I modify the Request.Headers collection?

查看:29
本文介绍了我可以修改 Request.Headers 集合吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用第三方报告组件的 ASP.NET 站点.每当客户端浏览器未在请求标头中指定 User-Agent 时,此组件就会抛出 NullReferenceException 异常.

I have an ASP.NET site that uses a third-party reporting component. This component is misbehaving by throwing a NullReferenceException whenever the client browser is not specifying a User-Agent in the request headers.

这基本上是一个奇怪的场景,我只是想想出一个解决方法.我不知道谁/哪个客户端没有指定用户代理,这似乎是 IMO 的错误形式,但我们必须处理它产生的异常.我已经向第三方记录了一份关于他们报告组件中的错误的支持票,但我怀疑这条路线会有多大的成果.所以我的想法只是检测 User-Agent 何时为空白并将其默认为只是为了安抚报告组件.但是,我似乎无法更改 Request.Headers 集合中的任何内容.我得到以下异常:

It's basically an odd scenario that I'm just trying to come up with a workaround for. I do not know who/what client is not specifying a User-Agent, which seems like bad form IMO, but we have to deal with the exceptions it is generating. I have logged a support ticket with the third-party regarding the bug in their reporting component, but I have my doubts about how fruitful that route is going to be. So my thought was just to detect when the User-Agent is blank and default it to something just to appease the reporting component. However, I can't seem to change anything in the Request.Headers collection. I get the following exception:

Operation is not supported on this platform.

我开始相信我无法做到这一点.我了解为什么 ASP.NET 不允许这样做,但我还没有想出任何其他解决方法.

I'm starting to believe I'm not going to be able to do this. I understand why ASP.NET wouldn't allow this, but I haven't come up with any other workaround.

更新: 在 penfold 的建议下,我尝试将 User-Agent 添加到 Request.Headers 集合 使用 HttpModule.这将其添加到 Headers 集合中,但没有更新 Request.UserAgent 属性,这是导致报告组件失败的原因.我一直在查看 .NET Reflector 以确定如何设置该属性以便我可以更新它,但我还没有想出任何东西(我可以找到的不仅仅是一个驱动属性的私有字段).

Update: At penfold's suggestion, I tried to add the User-Agent to the Request.Headers collection using an HttpModule. This got it added to the Headers collection, but did nothing to update the Request.UserAgent property, which is what is causing the reporting component to fail. I've been looking through .NET Reflector to determine how that property is set so that I can update it, but I haven't come up with anything yet (there isn't just a private field that drives the property that I can find).

推荐答案

我认为处理这个问题的最好方法是使用一个 http 模块,该模块将检查标头并在必要时注入用户代理.

I think the best way of handling this is to use a http module that will check the header and inject the user agent if necessary.

正如您所发现的,您不能在 Headers 对象上使用 set 方法.相反,您必须通过可通过反射访问的受保护属性将用户代理字符串注入标头,如 这个问题.

As you have found out you cannot use the set method on the Headers object. Instead you will have to inject the user agent string into the header via protected properties that can be accessed through reflection as outlined in the answer to this question.

不幸的是,Request.UserAgent 没有使用 Request.Headers 中保存的信息,而是调用 GetKnownRequestHeader="http://msdn.microsoft.com/en-us/library/system.web.httpworkerrequest%28v=vs.100%29.aspx" rel="nofollow noreferrer">HttpWorkerRequest.这是一个抽象类,通过查看反编译的库代码,实际实现会因托管环境而异.因此,我看不到通过反射以可靠方式替换用户代理字符串的方法.您可以推出自己的 WorkerRequest 课程,但我认为付出的努力并不值得.

Unfortunately Request.UserAgent doesn't use the information held in Request.Headers, instead it calls the method GetKnownRequestHeader in HttpWorkerRequest. This is an abstract class and from looking at the decompiled library code the actual implementation varies depending on the hosting environment. Therefore I cannot see a way to replace the user agent string in a reliable manner via reflection. You could roll your own WorkerRequest class but for the amount of effort I don't think the payoff would be worth it.

抱歉,我认为这是不可能的,但我认为不可能以简单的方式在 Web 应用程序中设置用户代理.目前最好的选择是对用户代理执行预检查,如果请求没有,则返回浏览器不支持的错误消息.

Sorry to be negative but I think its just not possible to set the user agent within the web application in a simple manner. Your best option at the moment would be to perform a pre-check for a user agent, and if the request doesn't have one return a browser not supported error message.

您还可以调查早期注入的东西,即在 IIS 中或在您的代理服务器上(如果您使用的话).

You could also investigate injecting something earlier on, i.e. in IIS or at your proxy server if you use one.

我还建议将此问题报告给 SAP.我知道他们目前正在积极开发 Viewer,谁知道他们可能会修复它,甚至可能会添加对 Opera 的支持!

Also I would recommend that this issue is reported to SAP. I know they are actively working on the Viewer at the moment and who knows they might fix it and maybe even add support for Opera!

这篇关于我可以修改 Request.Headers 集合吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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