mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
868a0e6c3c
commit
5a677be852
@ -12,6 +12,9 @@
|
||||
|
||||
package org.dromara.hutool.http;
|
||||
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.map.CaseInsensitiveMap;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.net.url.UrlQueryUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.http.client.ClientConfig;
|
||||
@ -23,6 +26,7 @@ import org.dromara.hutool.http.meta.Method;
|
||||
import org.dromara.hutool.http.server.SimpleServer;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -269,6 +273,7 @@ public class HttpUtil {
|
||||
|
||||
/**
|
||||
* 打印{@link Response} 为可读形式
|
||||
*
|
||||
* @param response {@link Response}
|
||||
* @return 字符串
|
||||
*/
|
||||
@ -285,4 +290,26 @@ public class HttpUtil {
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定的Header值,如果不存在返回{@code null}<br>
|
||||
* 根据RFC2616规范,header的name不区分大小写,因此首先get值,不存在则遍历匹配不区分大小写的key。
|
||||
*
|
||||
* @param headers 头信息的Map
|
||||
* @param name header名
|
||||
* @return header值
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static String header(final Map<String, ? extends Collection<String>> headers, final String name) {
|
||||
Collection<String> values = headers.get(name);
|
||||
if (null == values && !(headers instanceof CaseInsensitiveMap)) {
|
||||
// issue#I96U4T,根据RFC2616规范,header的name不区分大小写
|
||||
values = MapUtil.firstMatchValue(headers, entry -> StrUtil.equalsIgnoreCase(name, entry.getKey()));
|
||||
}
|
||||
if (CollUtil.isNotEmpty(values)) {
|
||||
return CollUtil.getFirst(values);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.meta.HeaderName;
|
||||
|
||||
import java.net.HttpCookie;
|
||||
@ -71,17 +72,7 @@ public interface HeaderOperation<T extends HeaderOperation<T>> {
|
||||
* @return header值
|
||||
*/
|
||||
default String header(final String name) {
|
||||
final Map<String, ? extends Collection<String>> headers = headers();
|
||||
Collection<String> values = headers.get(name);
|
||||
if(null == values){
|
||||
// issue#I96U4T,根据RFC2616规范,header的name不区分大小写
|
||||
values = MapUtil.firstMatchValue(headers, entry-> StrUtil.equalsIgnoreCase(name, entry.getKey()));
|
||||
}
|
||||
if (ArrayUtil.isNotEmpty(values)) {
|
||||
return CollUtil.getFirst(values);
|
||||
}
|
||||
|
||||
return null;
|
||||
return HttpUtil.header(headers(), name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,9 +12,7 @@
|
||||
|
||||
package org.dromara.hutool.http.client.engine.jdk;
|
||||
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.net.url.UrlUtil;
|
||||
import org.dromara.hutool.core.reflect.FieldUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
@ -32,7 +30,6 @@ import java.net.HttpURLConnection;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -295,13 +292,8 @@ public class JdkHttpConnection implements HeaderOperation<JdkHttpConnection>, Cl
|
||||
*/
|
||||
@Override
|
||||
public String header(final String name) {
|
||||
String headerField = this.conn.getHeaderField(name);
|
||||
if (null == headerField) {
|
||||
final Map<String, ? extends Collection<String>> headers = headers();
|
||||
headerField = CollUtil.getFirst(MapUtil.firstMatchValue(headers, entry -> StrUtil.equalsIgnoreCase(name, entry.getKey())));
|
||||
}
|
||||
|
||||
return headerField;
|
||||
// getHeaderField已经实现忽略大小写
|
||||
return this.conn.getHeaderField(name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,11 +12,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;
|
||||
@ -96,15 +93,7 @@ public class JdkHttpResponse implements Response, Closeable {
|
||||
|
||||
@Override
|
||||
public String header(final String 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);
|
||||
}
|
||||
return null;
|
||||
return HttpUtil.header(this.headers, name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user