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

查看:423
本文介绍了我可以修改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.

这是一个奇怪的情况,我只是想出了一个解决方法。我不知道谁/什么客户端没有指定一个User-Agent,这似乎是不好的形式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.



我开始相信我不能这样做。我明白为什么

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 a href =http://stackoverflow.com/a/4098734/249144>使用 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.

$ c> Request.UserAgent 不使用 Request.Headers 中保存的信息,而是调用 GetKnownRequestHeader 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。我知道他们现在正在积极工作的观众,谁知道他们可能解决它,甚至可以添加对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天全站免登陆