mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!1156 【轻量级pr】V6 httpUtil-createRequest从5.x迁移过来,方便使用
* HttpUtil类增加createRequest方法,方便使用(迁移从hutool5.x)
This commit is contained in:
parent
d4d81e3dd2
commit
2959e1798d
@ -54,6 +54,40 @@ public class HttpUtil {
|
|||||||
return StrUtil.startWithIgnoreCase(url, "http:");
|
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请求
|
* 发送get请求
|
||||||
*
|
*
|
||||||
|
@ -14,6 +14,7 @@ package org.dromara.hutool.http;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.io.file.FileUtil;
|
import org.dromara.hutool.core.io.file.FileUtil;
|
||||||
import org.dromara.hutool.core.lang.Console;
|
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.regex.ReUtil;
|
||||||
import org.dromara.hutool.core.util.CharsetUtil;
|
import org.dromara.hutool.core.util.CharsetUtil;
|
||||||
import org.dromara.hutool.http.client.Request;
|
import org.dromara.hutool.http.client.Request;
|
||||||
@ -26,6 +27,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
public class HttpUtilTest {
|
public class HttpUtilTest {
|
||||||
@ -188,4 +190,100 @@ public class HttpUtilTest {
|
|||||||
final String body = HttpUtil.send(Request.of(url)).bodyStr();
|
final String body = HttpUtil.send(Request.of(url)).bodyStr();
|
||||||
Console.log(body);
|
Console.log(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Disabled
|
||||||
|
public void httpUtilCreateRequest1PostSoap11Test(){
|
||||||
|
String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||||
|
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
|
||||||
|
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
|
||||||
|
" <soap:Body>\n" +
|
||||||
|
" <getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">\n" +
|
||||||
|
" <theIpAddress>222.91.66.232</theIpAddress>\n" +
|
||||||
|
" </getCountryCityByIp>\n" +
|
||||||
|
" </soap:Body>\n" +
|
||||||
|
"</soap:Envelope>";
|
||||||
|
|
||||||
|
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 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
||||||
|
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
|
||||||
|
" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
|
||||||
|
" <soap12:Body>\n" +
|
||||||
|
" <getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">\n" +
|
||||||
|
" <theIpAddress>222.91.66.232</theIpAddress>\n" +
|
||||||
|
" </getCountryCityByIp>\n" +
|
||||||
|
" </soap12:Body>\n" +
|
||||||
|
"</soap12:Envelope>";
|
||||||
|
|
||||||
|
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<String, Object> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user