mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add session
This commit is contained in:
@@ -17,6 +17,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.http.client.ClientConfig;
|
||||
import org.dromara.hutool.http.client.Request;
|
||||
import org.dromara.hutool.http.client.Response;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngineFactory;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.dromara.hutool.http.server.SimpleServer;
|
||||
@@ -85,8 +86,8 @@ public class HttpUtil {
|
||||
@SuppressWarnings("resource")
|
||||
public static String get(final String urlString, final int timeout) {
|
||||
return ClientEngineFactory.getEngine()
|
||||
.init(ClientConfig.of().setConnectionTimeout(timeout).setReadTimeout(timeout))
|
||||
.send(Request.of(urlString)).bodyStr();
|
||||
.init(ClientConfig.of().setConnectionTimeout(timeout).setReadTimeout(timeout))
|
||||
.send(Request.of(urlString)).bodyStr();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +100,7 @@ public class HttpUtil {
|
||||
@SuppressWarnings("resource")
|
||||
public static String get(final String urlString, final Map<String, Object> paramMap) {
|
||||
return send(Request.of(urlString).form(paramMap))
|
||||
.bodyStr();
|
||||
.bodyStr();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +113,7 @@ public class HttpUtil {
|
||||
@SuppressWarnings("resource")
|
||||
public static String post(final String urlString, final Map<String, Object> paramMap) {
|
||||
return send(Request.of(urlString).method(Method.POST).form(paramMap))
|
||||
.bodyStr();
|
||||
.bodyStr();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,7 +132,7 @@ public class HttpUtil {
|
||||
@SuppressWarnings("resource")
|
||||
public static String post(final String urlString, final String body) {
|
||||
return send(Request.of(urlString).method(Method.POST).body(body))
|
||||
.bodyStr();
|
||||
.bodyStr();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,18 +141,18 @@ public class HttpUtil {
|
||||
* @param request HTTP请求
|
||||
* @return HTTP响应
|
||||
*/
|
||||
public static Response send(final Request request){
|
||||
public static Response send(final Request request) {
|
||||
return ClientEngineFactory.getEngine().send(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将表单数据加到URL中(用于GET表单提交)
|
||||
* 表单的键值对会被url编码,但是url中原参数不会被编码
|
||||
* 且对form参数进行 FormUrlEncoded ,x-www-form-urlencoded模式,此模式下空格会编码为'+'
|
||||
* 且对form参数进行 FormUrlEncoded ,x-www-form-urlencoded模式,此模式下空格会编码为'+'
|
||||
*
|
||||
* @param url URL
|
||||
* @param form 表单数据
|
||||
* @param charset 编码 null表示不encode键值对
|
||||
* @param url URL
|
||||
* @param form 表单数据
|
||||
* @param charset 编码 null表示不encode键值对
|
||||
* @return 合成后的URL
|
||||
*/
|
||||
public static String urlWithFormUrlEncoded(final String url, final Map<String, Object> form, final Charset charset) {
|
||||
@@ -213,6 +214,16 @@ public class HttpUtil {
|
||||
return urlBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建客户端引擎
|
||||
*
|
||||
* @param engineName 引擎名称
|
||||
* @return {@link ClientEngine}
|
||||
*/
|
||||
public static ClientEngine createClient(final String engineName) {
|
||||
return ClientEngineFactory.createEngine(engineName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建简易的Http服务器
|
||||
*
|
||||
|
@@ -13,6 +13,7 @@
|
||||
package org.dromara.hutool.http.client.engine;
|
||||
|
||||
import org.dromara.hutool.core.lang.Singleton;
|
||||
import org.dromara.hutool.core.spi.ServiceLoader;
|
||||
import org.dromara.hutool.core.spi.SpiUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.http.HttpException;
|
||||
@@ -48,6 +49,26 @@ public class ClientEngineFactory {
|
||||
return createEngine().init(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建自定义引擎
|
||||
*
|
||||
* @param engineName 引擎名称,忽略大小写,如`HttpClient4`、`HttpClient5`、`OkHttp`、`JdkClient`
|
||||
* @return 引擎
|
||||
* @throws HttpException 无对应名称的引擎
|
||||
*/
|
||||
public static ClientEngine createEngine(String engineName) throws HttpException {
|
||||
if (!StrUtil.endWithIgnoreCase(engineName, "Engine")) {
|
||||
engineName = engineName + "Engine";
|
||||
}
|
||||
final ServiceLoader<ClientEngine> list = SpiUtil.loadList(ClientEngine.class);
|
||||
for (final String serviceName : list.getServiceNames()) {
|
||||
if (StrUtil.endWithIgnoreCase(serviceName, engineName)) {
|
||||
return list.getService(serviceName);
|
||||
}
|
||||
}
|
||||
throw new HttpException("No such engine named: " + engineName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户引入的HTTP客户端引擎jar,自动创建对应的拼音引擎对象<br>
|
||||
* 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎
|
||||
|
@@ -13,8 +13,8 @@
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.httpclient4.HttpClient4Engine;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -25,7 +25,7 @@ public class HttpClient4EngineTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void getTest() {
|
||||
final ClientEngine engine = new HttpClient4Engine();
|
||||
final ClientEngine engine = HttpUtil.createClient("httpclient4");
|
||||
|
||||
final Request req = Request.of("https://www.hutool.cn/").method(Method.GET);
|
||||
final Response res = engine.send(req);
|
||||
|
@@ -13,8 +13,8 @@
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.httpclient5.HttpClient5Engine;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -25,7 +25,7 @@ public class HttpClient5EngineTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void getTest() {
|
||||
final ClientEngine engine = new HttpClient5Engine();
|
||||
final ClientEngine engine = HttpUtil.createClient("httpclient5");
|
||||
|
||||
final Request req = Request.of("https://www.hutool.cn/").method(Method.GET);
|
||||
final Response res = engine.send(req);
|
||||
|
@@ -13,9 +13,8 @@
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.httpclient4.HttpClient4Engine;
|
||||
import org.dromara.hutool.http.client.engine.okhttp.OkHttpEngine;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -25,7 +24,7 @@ public class Issue3240Test {
|
||||
@Disabled
|
||||
void okHttpTest() {
|
||||
String url = "https://gh.yubue.cn/https://github.com/espressif/arduino-esp32/releases/download/2.0.11/package_esp32_dev_index.json";
|
||||
final ClientEngine engine = new OkHttpEngine();
|
||||
final ClientEngine engine = HttpUtil.createClient("okhttp");
|
||||
final Response send = engine.send(Request.of(url).method(Method.GET));
|
||||
Console.log(send.body().getString());
|
||||
}
|
||||
@@ -34,7 +33,7 @@ public class Issue3240Test {
|
||||
@Disabled
|
||||
void httpClient4Test() {
|
||||
String url = "https://gh.yubue.cn/https://github.com/espressif/arduino-esp32/releases/download/2.0.11/package_esp32_dev_index.json";
|
||||
final ClientEngine engine = new HttpClient4Engine();
|
||||
final ClientEngine engine = HttpUtil.createClient("okhttp");
|
||||
final Response send = engine.send(Request.of(url).method(Method.GET));
|
||||
Console.log(send.body().getString());
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IssueI7ZRJUTest {
|
||||
|
||||
@SuppressWarnings({"resource", "TestFailedLine"})
|
||||
@SuppressWarnings({"resource"})
|
||||
@Test
|
||||
@Disabled
|
||||
void getBadSSlTest() {
|
||||
|
@@ -13,8 +13,8 @@
|
||||
package org.dromara.hutool.http.client;
|
||||
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.http.HttpUtil;
|
||||
import org.dromara.hutool.http.client.engine.ClientEngine;
|
||||
import org.dromara.hutool.http.client.engine.jdk.JdkClientEngine;
|
||||
import org.dromara.hutool.http.meta.Method;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -24,7 +24,7 @@ public class JdkEngineTest {
|
||||
@Test
|
||||
@Disabled
|
||||
public void getTest(){
|
||||
final ClientEngine engine = new JdkClientEngine();
|
||||
final ClientEngine engine = HttpUtil.createClient("jdkClient");
|
||||
|
||||
final Request req = Request.of("https://www.hutool.cn/").method(Method.GET);
|
||||
final Response res = engine.send(req);
|
||||
|
Reference in New Issue
Block a user