java HttpServletRequest 和 getHeader():如何正确处理不区分大小写的标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1379648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpServletRequest and getHeader(): How to handle case insensitive headers properly?
提问by Malax
Since I discovered that HTTP headers are case-insensive according to the RFC, i was wondering how I can access HTTP headers in a case-insensitive way with Servlets. There is a #getHeader(String) method to obtain a header but it turned out that this method treats the header fields case sensitive.
由于我发现根据 RFC 的 HTTP 标头不区分大小写,我想知道如何使用 Servlet 以不区分大小写的方式访问 HTTP 标头。有一个 #getHeader(String) 方法可以获取标题,但结果证明该方法对标题字段区分大小写。
Is there a "case insensitive" way to get header fields ? Or do i have to iterate over all the header fields to find the header field I was looking for ?
是否有“不区分大小写”的方式来获取标题字段?或者我是否必须遍历所有标题字段才能找到我正在寻找的标题字段?
回答by Jon Skeet
Which servlet container are you using? The docs for getHeader(String)state:
您使用的是哪个 servlet 容器?在对文档getHeader(String)状态:
The header name is case insensitive.
标题名称不区分大小写。
so it sounds like a bug in the container you're using.
所以这听起来像是您正在使用的容器中的错误。
回答by abdel
tomcat 8.0.24 impl of getHeader delegates to 'org.apache.tomcat.util.http.MimeHeaders' which eventually calls this method below which in turn does case insensitive check
getHeader 的 tomcat 8.0.24 impl 委托给“org.apache.tomcat.util.http.MimeHeaders”,它最终在下面调用此方法,然后进行不区分大小写的检查
313 public MessageBytes getValue(String name) {

