mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
get support body
This commit is contained in:
parent
5ee0a73ebb
commit
c62ede5276
@ -10,6 +10,7 @@
|
||||
* 【core 】 ReflectUtil.getFieldValue支持static(issue#662@Github)
|
||||
* 【core 】 改进Bean判断和注入逻辑:支持public字段注入(issue#I1689L@Gitee)
|
||||
* 【extra】 新增SpringUtil
|
||||
* 【http 】 Get请求支持body,移除body(JSON)方法(issue#671@Github)
|
||||
|
||||
|
||||
### Bug修复
|
||||
|
@ -26,6 +26,7 @@
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-json</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.xml.soap</groupId>
|
||||
|
@ -91,6 +91,9 @@ public class HttpConnection {
|
||||
throw new HttpException(e);
|
||||
}
|
||||
|
||||
// 默认读取响应内容
|
||||
this.conn.setDoInput(true);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -118,13 +121,10 @@ public class HttpConnection {
|
||||
throw new HttpException(e);
|
||||
}
|
||||
|
||||
// do input and output
|
||||
this.conn.setDoInput(true);
|
||||
if (Method.POST.equals(method) //
|
||||
|| Method.PUT.equals(method)//
|
||||
|| Method.PATCH.equals(method)//
|
||||
|| Method.DELETE.equals(method)) {
|
||||
this.conn.setDoOutput(true);
|
||||
this.conn.setUseCaches(false);
|
||||
}
|
||||
return this;
|
||||
@ -285,6 +285,7 @@ public class HttpConnection {
|
||||
* 关闭缓存
|
||||
*
|
||||
* @return this
|
||||
* @see HttpURLConnection#setUseCaches(boolean)
|
||||
*/
|
||||
public HttpConnection disableCache() {
|
||||
this.conn.setUseCaches(false);
|
||||
@ -447,6 +448,9 @@ public class HttpConnection {
|
||||
if (null == this.conn) {
|
||||
throw new IOException("HttpURLConnection has not been initialized.");
|
||||
}
|
||||
|
||||
// 当有写出需求时,自动打开之
|
||||
this.conn.setDoOutput(true);
|
||||
return this.conn.getOutputStream();
|
||||
}
|
||||
|
||||
@ -511,7 +515,7 @@ public class HttpConnection {
|
||||
// --------------------------------------------------------------- Private Method start
|
||||
/**
|
||||
* 初始化http或https请求参数<br>
|
||||
* 有些时候htts请求会出现com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl的实现,此为sun内部api,按照普通http请求处理
|
||||
* 有些时候https请求会出现com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl的实现,此为sun内部api,按照普通http请求处理
|
||||
*
|
||||
* @return {@link HttpURLConnection},https返回{@link HttpsURLConnection}
|
||||
*/
|
||||
|
@ -5,13 +5,20 @@ import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.resource.*;
|
||||
import cn.hutool.core.io.resource.BytesResource;
|
||||
import cn.hutool.core.io.resource.FileResource;
|
||||
import cn.hutool.core.io.resource.MultiFileResource;
|
||||
import cn.hutool.core.io.resource.MultiResource;
|
||||
import cn.hutool.core.io.resource.Resource;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.*;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.URLUtil;
|
||||
import cn.hutool.http.cookie.GlobalCookieManager;
|
||||
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
|
||||
import cn.hutool.json.JSON;
|
||||
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
@ -19,7 +26,11 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.*;
|
||||
import java.net.CookieManager;
|
||||
import java.net.HttpCookie;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.Proxy;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@ -687,19 +698,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置JSON内容主体<br>
|
||||
* 设置默认的Content-Type为 application/json 需在此方法调用前使用charset方法设置编码,否则使用默认编码UTF-8
|
||||
*
|
||||
* @param json JSON请求体
|
||||
* @return this
|
||||
* @deprecated 未来可能去除此方法,使用{@link #body(String)} 传入JSON字符串即可
|
||||
*/
|
||||
@Deprecated
|
||||
public HttpRequest body(JSON json) {
|
||||
return this.body(json.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置主体字节码<br>
|
||||
* 需在此方法调用前使用charset方法设置编码,否则使用默认编码UTF-8
|
||||
@ -870,7 +868,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否rest模式
|
||||
* 设置是否rest模式<br>
|
||||
* rest模式下get请求不会把参数附加到URL之后
|
||||
*
|
||||
* @param isRest 是否rest模式
|
||||
* @return this
|
||||
@ -1055,7 +1054,10 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
||||
*/
|
||||
private void send() throws IORuntimeException {
|
||||
try {
|
||||
if (Method.POST.equals(this.method) || Method.PUT.equals(this.method) || Method.DELETE.equals(this.method) || this.isRest) {
|
||||
if (Method.POST.equals(this.method) //
|
||||
|| Method.PUT.equals(this.method) //
|
||||
|| Method.DELETE.equals(this.method) //
|
||||
|| this.isRest) {
|
||||
if (CollectionUtil.isEmpty(this.fileForm)) {
|
||||
sendFormUrlEncoded();// 普通表单
|
||||
} else {
|
||||
|
@ -15,22 +15,21 @@ import cn.hutool.json.JSONUtil;
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public class RestTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void contentTypeTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2").toString());
|
||||
Assert.assertEquals("application/json;charset=UTF-8", request.header("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@SuppressWarnings("deprecation")
|
||||
public void postTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@ -44,10 +43,9 @@ public class RestTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
@SuppressWarnings("deprecation")
|
||||
public void postTest3() {
|
||||
HttpRequest request = HttpRequest.post("http://211.162.39.204:8181/jeesite-simple/a/open/bizGwbnService/test")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user