From 2959e1798d83b687074eb853b68cde095dc933de Mon Sep 17 00:00:00 2001 From: dazer007 Date: Wed, 17 Jan 2024 03:01:12 +0000 Subject: [PATCH] =?UTF-8?q?!1156=20=E3=80=90=E8=BD=BB=E9=87=8F=E7=BA=A7pr?= =?UTF-8?q?=E3=80=91V6=20httpUtil-createRequest=E4=BB=8E5.x=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=BF=87=E6=9D=A5=EF=BC=8C=E6=96=B9=E4=BE=BF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20*=20HttpUtil=E7=B1=BB=E5=A2=9E=E5=8A=A0createReques?= =?UTF-8?q?t=E6=96=B9=E6=B3=95=EF=BC=8C=E6=96=B9=E4=BE=BF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=EF=BC=88=E8=BF=81=E7=A7=BB=E4=BB=8Ehutool5.x=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/http/HttpUtil.java | 34 +++++++ .../org/dromara/hutool/http/HttpUtilTest.java | 98 +++++++++++++++++++ 2 files changed, 132 insertions(+) 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); + } }