mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix http bug
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -6,18 +6,21 @@ 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;
|
||||
|
||||
/**
|
||||
* {@link HttpRequest}单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class HttpRequestTest {
|
||||
final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2";
|
||||
@@ -43,7 +46,7 @@ public class HttpRequestTest {
|
||||
@Ignore
|
||||
public void toStringTest() {
|
||||
String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
|
||||
|
||||
|
||||
HttpRequest request = HttpRequest.get(url).body("a=乌海");
|
||||
Console.log(request.toString());
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user