get support body

This commit is contained in:
Looly 2019-12-17 09:32:57 +08:00
parent 5ee0a73ebb
commit c62ede5276
5 changed files with 35 additions and 29 deletions

View File

@ -10,6 +10,7 @@
* 【core 】 ReflectUtil.getFieldValue支持staticissue#662@Github * 【core 】 ReflectUtil.getFieldValue支持staticissue#662@Github
* 【core 】 改进Bean判断和注入逻辑支持public字段注入issue#I1689L@Gitee * 【core 】 改进Bean判断和注入逻辑支持public字段注入issue#I1689L@Gitee
* 【extra】 新增SpringUtil * 【extra】 新增SpringUtil
* 【http 】 Get请求支持body移除bodyJSON方法issue#671@Github
### Bug修复 ### Bug修复

View File

@ -26,6 +26,7 @@
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId> <artifactId>hutool-json</artifactId>
<version>${project.parent.version}</version> <version>${project.parent.version}</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.xml.soap</groupId> <groupId>javax.xml.soap</groupId>

View File

@ -91,6 +91,9 @@ public class HttpConnection {
throw new HttpException(e); throw new HttpException(e);
} }
// 默认读取响应内容
this.conn.setDoInput(true);
return this; return this;
} }
@ -118,13 +121,10 @@ public class HttpConnection {
throw new HttpException(e); throw new HttpException(e);
} }
// do input and output
this.conn.setDoInput(true);
if (Method.POST.equals(method) // if (Method.POST.equals(method) //
|| Method.PUT.equals(method)// || Method.PUT.equals(method)//
|| Method.PATCH.equals(method)// || Method.PATCH.equals(method)//
|| Method.DELETE.equals(method)) { || Method.DELETE.equals(method)) {
this.conn.setDoOutput(true);
this.conn.setUseCaches(false); this.conn.setUseCaches(false);
} }
return this; return this;
@ -285,6 +285,7 @@ public class HttpConnection {
* 关闭缓存 * 关闭缓存
* *
* @return this * @return this
* @see HttpURLConnection#setUseCaches(boolean)
*/ */
public HttpConnection disableCache() { public HttpConnection disableCache() {
this.conn.setUseCaches(false); this.conn.setUseCaches(false);
@ -447,6 +448,9 @@ public class HttpConnection {
if (null == this.conn) { if (null == this.conn) {
throw new IOException("HttpURLConnection has not been initialized."); throw new IOException("HttpURLConnection has not been initialized.");
} }
// 当有写出需求时自动打开之
this.conn.setDoOutput(true);
return this.conn.getOutputStream(); return this.conn.getOutputStream();
} }
@ -511,7 +515,7 @@ public class HttpConnection {
// --------------------------------------------------------------- Private Method start // --------------------------------------------------------------- Private Method start
/** /**
* 初始化http或https请求参数<br> * 初始化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} * @return {@link HttpURLConnection}https返回{@link HttpsURLConnection}
*/ */

View File

@ -5,13 +5,20 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; 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.lang.Assert;
import cn.hutool.core.map.MapUtil; 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.cookie.GlobalCookieManager;
import cn.hutool.http.ssl.SSLSocketFactoryBuilder; import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
import cn.hutool.json.JSON;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
@ -19,7 +26,11 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; 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.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -687,19 +698,6 @@ public class HttpRequest extends HttpBase<HttpRequest> {
return this; 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> * 设置主体字节码<br>
* 需在此方法调用前使用charset方法设置编码否则使用默认编码UTF-8 * 需在此方法调用前使用charset方法设置编码否则使用默认编码UTF-8
@ -870,7 +868,8 @@ public class HttpRequest extends HttpBase<HttpRequest> {
} }
/** /**
* 设置是否rest模式 * 设置是否rest模式<br>
* rest模式下get请求不会把参数附加到URL之后
* *
* @param isRest 是否rest模式 * @param isRest 是否rest模式
* @return this * @return this
@ -1055,7 +1054,10 @@ public class HttpRequest extends HttpBase<HttpRequest> {
*/ */
private void send() throws IORuntimeException { private void send() throws IORuntimeException {
try { 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)) { if (CollectionUtil.isEmpty(this.fileForm)) {
sendFormUrlEncoded();// 普通表单 sendFormUrlEncoded();// 普通表单
} else { } else {

View File

@ -15,22 +15,21 @@ import cn.hutool.json.JSONUtil;
* @author looly * @author looly
* *
*/ */
@SuppressWarnings("ConstantConditions")
public class RestTest { public class RestTest {
@Test @Test
@SuppressWarnings("deprecation")
public void contentTypeTest() { public void contentTypeTest() {
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")// 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")); Assert.assertEquals("application/json;charset=UTF-8", request.header("Content-Type"));
} }
@Test @Test
@Ignore @Ignore
@SuppressWarnings("deprecation")
public void postTest() { public void postTest() {
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")// 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()); Console.log(request.execute().body());
} }
@ -44,10 +43,9 @@ public class RestTest {
@Test @Test
@Ignore @Ignore
@SuppressWarnings("deprecation")
public void postTest3() { public void postTest3() {
HttpRequest request = HttpRequest.post("http://211.162.39.204:8181/jeesite-simple/a/open/bizGwbnService/test")// 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()); Console.log(request.execute().body());
} }
} }