diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/HttpUtil.java b/hutool-http/src/main/java/org/dromara/hutool/http/HttpUtil.java index 1d2db5013..06c6b6042 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/HttpUtil.java @@ -54,6 +54,40 @@ public class HttpUtil { return StrUtil.startWithIgnoreCase(url, "http:"); } + /** + * 创建Http请求对象 + * + * @param method 方法枚举{@link Method} + * @param url 请求的URL,可以使HTTP或者HTTPS + * @return {@link Request} + * @since 3.0.9 + */ + public static Request createRequest(String url, Method method) { + return Request.of(url).method(method); + } + + /** + * 创建Http GET请求对象 + * + * @param url 请求的URL,可以使HTTP或者HTTPS + * @return {@link Request} + * @since 3.2.0 + */ + public static Request createGet(String url) { + return createRequest(url, Method.GET); + } + + /** + * 创建Http POST请求对象 + * + * @param url 请求的URL,可以使HTTP或者HTTPS + * @return {@link Request} + * @since 3.2.0 + */ + public static Request createPost(String url) { + return createRequest(url, Method.POST); + } + /** * 发送get请求 * diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java index bd8212d7b..280428906 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/HttpUtilTest.java @@ -14,6 +14,7 @@ package org.dromara.hutool.http; import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.lang.Console; +import org.dromara.hutool.core.map.Dict; import org.dromara.hutool.core.regex.ReUtil; import org.dromara.hutool.core.util.CharsetUtil; import org.dromara.hutool.http.client.Request; @@ -26,6 +27,7 @@ import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; @SuppressWarnings("resource") public class HttpUtilTest { @@ -188,4 +190,100 @@ public class HttpUtilTest { final String body = HttpUtil.send(Request.of(url)).bodyStr(); Console.log(body); } + + + @Test + @Disabled + public void httpUtilCreateRequest1PostSoap11Test(){ + String requestBody = "\n" + + "\n" + + " \n" + + " \n" + + " 222.91.66.232\n" + + " \n" + + " \n" + + ""; + + String body = HttpUtil.createRequest("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx", Method.POST) + .header(HeaderName.CONTENT_TYPE, "text/xml; charset=utf-8") + .header("Accept", "application/xml") + .header("accessId", "") + .header("Authorization", "") + .header("Cookie", "") + ///.timeout(1, TimeUnit.SECONDS) + .body(requestBody) + .send() + .body(). + getString(); + Console.log(body); + } + + @Test + @Disabled + public void httpUtilCreateRequest2PostSoap12Test(){ + String requestBody = "\n" + + "\n" + + " \n" + + " \n" + + " 222.91.66.232\n" + + " \n" + + " \n" + + ""; + + String body = HttpUtil.createPost("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx") + .header(HeaderName.CONTENT_TYPE, "application/soap+xml; charset=utf-8") + .header("Accept", "application/xml") + .header("accessId", "") + .header("Authorization", "") + .header("Cookie", "") + ///.timeout(1, TimeUnit.SECONDS) + .body(requestBody) + .send() + .body(). + getString(); + Console.log(body); + } + + @Test + @Disabled + public void httpUtilCreateRequest3GetTest(){ + final Map formMap = Dict.ofKvs( + "q1", "v1", + "q2", "v2") + ; + //设置超时 + HttpGlobalConfig.setTimeout(1); + String body = HttpUtil.createGet("https://echo.apifox.com/get") + .header(HeaderName.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=utf-8") + .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + .header("Accept", "*/*") + .header("Host", "echo.apifox.com") + .header("Connection", "keep-alive") + //.timeout(1, TimeUnit.SECONDS) + .form(formMap) + .send() + .body(). + getString(); + Console.log(body); + } + + @Test + @Disabled + public void httpUtilCreateRequest4PostTest(){ + String requestBodyJson = "{\n\"username\": \"张三\",\n \"password\": \"abcdefg@123\"\n}"; + String body = HttpUtil.createPost("https://echo.apifox.com/post?q1=v1&q2=v2") + .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + .header("Content-Type", "application/json") + .header("Accept", "*/*") + .header("Host", "echo.apifox.com") + .header("Connection", "keep-alive") + ///.timeout(1, TimeUnit.SECONDS) + .body(requestBodyJson) + .send() + .body() + .getString(); + Console.log(body); + } }