简体   繁体   English

ASP.NET Web API:更改请求标头

[英]ASP.NET Web API: Change Request Headers

A JavaScript client that I have no control over is sending the incorrect HTTP request headers to my Web API services. 我无法控制的JavaScript客户端正在将错误的HTTP请求标头发送到我的Web API服务。 More specifically, it's using a library that is sending an incorrect OData header. 更具体地说,它使用的是发送错误OData标头的库。

Is there any way that I can intercept the HTTP request before it hits my services? 有什么方法可以在HTTP请求到达我的服务之前对其进行拦截? Can I add/remove/update headers or query string info? 我可以添加/删除/更新标题或查询字符串信息吗?

For instance, if I receive the following HTTP header: 例如,如果我收到以下HTTP标头:

GET /Some/API HTTP/1.1
Host: myhost.com:80
MaxDataServiceVersion: 2.0

I'd like to know how to modify it to the following before the OData libraries take over: 在OData库接管之前,我想知道如何将其修改为以下内容:

GET /Some/API HTTP/1.1
Host: myhost.com:80
MaxDataServiceVersion: 4.0

The header isn't incorrect. 标头不正确。 Your client expects an OData v2 service and even if you did manipulate the headers, it probably won't be able to understand the response from your server. 您的客户端希望使用OData v2服务,即使您确实操作了标头,它也可能无法理解服务器的响应。

But you could use a simple HTTP proxy to rewrite the headers if you really want to try that route. 但是,如果您确实想尝试该路由,则可以使用简单的HTTP代理重写标头。

If you do that, make sure your OData server supports the Atom format because the OData JSON format changed completely between versions 2 and 4, so there's no way that JavaScript client will understand it. 如果这样做,请确保OData服务器支持Atom格式,因为OData JSON格式在版本2和版本4之间已完全更改,因此JavaScript客户端将无法理解它。 The Atom format changed as well, but if the client's parser is extremely lenient, it might work. Atom格式也发生了变化,但是如果客户端的解析器过于宽松,它可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM