fix http bug

This commit is contained in:
Looly 2020-09-05 20:07:06 +08:00
parent cbbf4671ba
commit 3e05c5b396
7 changed files with 85 additions and 15 deletions

View File

@ -21,6 +21,7 @@
* 【core 】 重新整理农历节假日解决一个pr过来的玩笑导致的问题
* 【poi 】 修复ExcelFileUtil.isXls判断问题pr#1055@Github
* 【poi 】 修复CglibUtil.copyList参数错误导致的问题
* 【http 】 修复GET请求附带body导致变POST的问题
-------------------------------------------------------------------------------------------------------------

View File

@ -1350,7 +1350,7 @@ public class CollUtil {
* @return 抽取后的新列表
* @since 5.3.5
*/
public static <T, R> List<R> map(Iterable<T> collection, Function<T, R> func, boolean ignoreNull) {
public static <T, R> List<R> map(Iterable<T> collection, Function<? super T, ? extends R> func, boolean ignoreNull) {
final List<R> fieldValueList = new ArrayList<>();
if (null == collection) {
return fieldValueList;

View File

@ -11,7 +11,17 @@ import cn.hutool.core.lang.Matcher;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
/**
* 数组工具类
@ -4274,4 +4284,23 @@ public class ArrayUtil {
public static <T> boolean isAllNotNull(T... array) {
return false == hasNull(array);
}
/**
* 按照指定规则将一种类型的数组转换为另一种类型
*
* @param array 被转换的数组
* @param targetComponentType 目标的元素类型
* @param func 转换规则函数
* @param <T> 原数组类型
* @param <R> 目标数组类型
* @return 转换后的数组
* @since 5.4.2
*/
public static <T, R> R[] map(T[] array, Class<R> targetComponentType, Function<? super T, ? extends R> func){
final R[] result = newArray(targetComponentType, array.length);
for(int i=0; i< array.length; i++){
result[i] = func.apply(array[i]);
}
return result;
}
}

View File

@ -295,7 +295,7 @@ public class TypeUtil {
* @return 给定泛型参数对应的实际类型如果无对应类型返回null
* @since 5.4.1
*/
public static TableMap<TypeVariable<?>, Type> getActualTypeMap(Type actualType, Class<?> typeDefineClass) {
public static TableMap<String, Type> getActualTypeMap(Type actualType, Class<?> typeDefineClass) {
if (false == typeDefineClass.isAssignableFrom(getClass(actualType))) {
throw new IllegalArgumentException("Parameter [superClass] must be assignable from [clazz]");
}
@ -305,13 +305,14 @@ public class TypeUtil {
if (ArrayUtil.isEmpty(typeVars)) {
return new TableMap<>(0);
}
// 实际类型列表
final Type[] actualTypeArguments = TypeUtil.getTypeArguments(actualType);
if (ArrayUtil.isEmpty(actualTypeArguments)) {
return new TableMap<>(0);
}
return new TableMap<>(typeVars, actualTypeArguments);
return new TableMap<>(ArrayUtil.map(typeVars, String.class, TypeVariable::getName), actualTypeArguments);
}
/**
@ -331,13 +332,14 @@ public class TypeUtil {
* @since 4.5.7
*/
public static Type[] getActualTypes(Type actualType, Class<?> typeDefineClass, Type... typeVariables) {
final TableMap<TypeVariable<?>, Type> tableMap = getActualTypeMap(actualType, typeDefineClass);
final TableMap<String, Type> tableMap = getActualTypeMap(actualType, typeDefineClass);
// 查找方法定义所在类或接口中此泛型参数的位置
final Type[] result = new Type[typeVariables.length];
for (int i = 0; i < typeVariables.length; i++) {
//noinspection SuspiciousMethodCalls
result[i] = (typeVariables[i] instanceof TypeVariable) ? tableMap.get(typeVariables[i]) : typeVariables[i];
result[i] = (typeVariables[i] instanceof TypeVariable)
? tableMap.get(((TypeVariable<?>) typeVariables[i]).getName())
: typeVariables[i];
}
return result;
}
@ -359,7 +361,7 @@ public class TypeUtil {
* @since 4.5.2
*/
public static Type getActualType(Type actualType, Class<?> typeDefineClass, Type typeVariable) {
Type[] types = getActualTypes(actualType, typeDefineClass, typeVariable);
final Type[] types = getActualTypes(actualType, typeDefineClass, typeVariable);
if (ArrayUtil.isNotEmpty(types)) {
return types[0];
}

View File

@ -60,10 +60,12 @@ public class TypeUtilTest {
@Test
public void getActualTypesTest(){
final Type id = TypeUtil.getActualType(
final Type idType = TypeUtil.getActualType(
Station.class,
Entity.class,
Tree.class,
TypeUtil.getFieldType(Station.class, "id"));
Assert.assertEquals(Long.class, idType);
}
public static class Station extends Tree<Station, Long>{

View File

@ -444,9 +444,21 @@ public class HttpConnection {
throw new IOException("HttpURLConnection has not been initialized.");
}
final Method method = getMethod();
// 当有写出需求时自动打开之
this.conn.setDoOutput(true);
return this.conn.getOutputStream();
final OutputStream out = this.conn.getOutputStream();
// 解决在Rest请求中GET请求附带body导致GET请求被强制转换为POST
// 在sun.net.www.protocol.http.HttpURLConnection.getOutputStream0方法中会把GET方法
// 修改为POST而且无法调用setRequestMethod方法修改因此此处使用反射强制修改字段属性值
// https://stackoverflow.com/questions/978061/http-get-with-request-body/983458
if(method == Method.GET && method != getMethod()){
ReflectUtil.setFieldValue(this.conn, "method", Method.GET.name());
}
return out;
}
/**

View File

@ -6,10 +6,14 @@ import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
import cn.hutool.json.JSONUtil;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -17,7 +21,6 @@ import java.util.Map;
* {@link HttpRequest}单元测试
*
* @author Looly
*
*/
public class HttpRequestTest {
final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2";
@ -105,8 +108,29 @@ public class HttpRequestTest {
@Test
@Ignore
public void bodyTest(){
public void bodyTest() {
String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().body();
Console.log(ddddd1);
}
/**
* 测试GET请求附带body体是否会变更为POST
*/
@Test
@Ignore
public void getLocalTest() {
List<String> list = new ArrayList<>();
list.add("hhhhh");
list.add("sssss");
Map<String, Object> map = new HashMap<>(16);
map.put("recordId", "12321321");
map.put("page", "1");
map.put("size", "2");
map.put("sizes", list);
String s = JSONUtil.toJsonStr(map);
HttpRequest request = HttpUtil.createGet("http://localhost:8888/get");
Console.log(request.execute().body());
}
}