gpt4 book ai didi

java - HttpHeaders getFirst() 不区分大小写

转载 作者:行者123 更新时间:2023-11-29 04:16:14 24 4
gpt4 key购买 nike

我正在使用 Spring 的 RestTemplate 获取 HTTP header 。

HTTP headers are case insensitive ,但是 documentation for HttpHeaders似乎不承认这一点。

快速测试表明一切按预期工作......

HttpHeaders headers = restTemplate.headForHeaders(url);
Long a = Long.parseLong(headers.getFirst("Content-Length"));
Long b = Long.parseLong(headers.getFirst("content-length"));
assert( a.equals(b) ); // passes

我能确定这个测试在所有 Spring 配置下都能通过吗?

最佳答案

根据 this commit明确指出 HttpHeaders不区分大小写:

Note that HttpHeaders generally treats header names in a case-insensitive manner.


旧答案

HttpHeaders 只有一个公共(public)构造函数及其主体:

public HttpHeaders() {
this(new LinkedCaseInsensitiveMap<>(8, Locale.ENGLISH), false);
}

并根据LinkedCaseInsensitiveMap docs :

LinkedHashMap variant that stores String keys in a case-insensitive manner, for example for key-based access in a results table.

Preserves the original order as well as the original casing of keys, while allowing for contains, get and remove calls with any case of key.

是的,它始终以不区分大小写的方式工作。

但为什么你不使用 HttpHeaders#getContentLength() ? :

// no need to convert String to long
long contentLength = httpHeaders.getContentLength();

关于java - HttpHeaders getFirst() 不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52224422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com