This commit is contained in:
Looly
2024-03-08 11:38:31 +08:00
parent 0adc4fd329
commit f78e4fecb8
2 changed files with 46 additions and 1 deletions

View File

@@ -1374,4 +1374,43 @@ public class MapUtil extends MapGetUtil {
}
return list;
}
/**
* 遍历Map返回第一个匹配的value值
*
* @param map map
* @param predicate 匹配条件
* @param <K> 键类型
* @param <V> 值类型
* @return value值
* @since 6.0.0
*/
public static <K, V> V firstMatchValue(final Map<K, V> map, final Predicate<Entry<K, V>> predicate) {
final Entry<K, V> kvEntry = firstMatch(map, predicate);
if (null != kvEntry) {
return kvEntry.getValue();
}
return null;
}
/**
* 遍历Map返回第一个匹配的entry值
*
* @param map map
* @param predicate 匹配条件
* @param <K> 键类型
* @param <V> 值类型
* @return entry
* @since 6.0.0
*/
public static <K, V> Entry<K, V> firstMatch(final Map<K, V> map, final Predicate<Entry<K, V>> predicate) {
if (isNotEmpty(map)) {
for (final Entry<K, V> entry : map.entrySet()) {
if (predicate.test(entry)) {
return entry;
}
}
}
return null;
}
}

View File

@@ -15,6 +15,8 @@ package org.dromara.hutool.http.client.engine.jdk;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.io.stream.EmptyInputStream;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.http.HttpException;
import org.dromara.hutool.http.HttpUtil;
@@ -94,7 +96,11 @@ public class JdkHttpResponse implements Response, Closeable {
@Override
public String header(final String name) {
final List<String> headerValues = this.headers.get(name);
List<String> headerValues = this.headers.get(name);
if(null == headerValues){
// issue#I96U4T根据RFC2616规范header的name不区分大小写
headerValues = MapUtil.firstMatchValue(this.headers, entry-> StrUtil.equalsIgnoreCase(name, entry.getKey()));
}
if (ArrayUtil.isNotEmpty(headerValues)) {
return headerValues.get(0);
}