mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
feat(hutool-http): 控制请求头是否聚合
This commit is contained in:
parent
4ef8814c14
commit
b9425a3b2b
@ -39,6 +39,10 @@ public abstract class HttpBase<T> {
|
|||||||
*/
|
*/
|
||||||
public static final String HTTP_1_1 = "HTTP/1.1";
|
public static final String HTTP_1_1 = "HTTP/1.1";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否聚合重复请求头
|
||||||
|
*/
|
||||||
|
protected boolean isHeaderAggregated = false;
|
||||||
/**
|
/**
|
||||||
* 存储头信息
|
* 存储头信息
|
||||||
*/
|
*/
|
||||||
@ -276,6 +280,26 @@ public abstract class HttpBase<T> {
|
|||||||
this.headers.clear();
|
this.headers.clear();
|
||||||
return (T) this;
|
return (T) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置是否需要聚合重复的请求头,将重复的请求头聚合为,拼接
|
||||||
|
*
|
||||||
|
* @param aggregate 是否需要聚合
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public T headerAggregation(boolean aggregate) {
|
||||||
|
this.isHeaderAggregated = aggregate;
|
||||||
|
return (T) this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取是否需要聚合请求头状态
|
||||||
|
*
|
||||||
|
* @return isHeaderAggregated 请求头聚合状态
|
||||||
|
*/
|
||||||
|
public boolean isHeaderAggregated() {
|
||||||
|
return isHeaderAggregated;
|
||||||
|
}
|
||||||
// ---------------------------------------------------------------- Headers end
|
// ---------------------------------------------------------------- Headers end
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -229,6 +229,31 @@ public class HttpConnection {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置请求头<br>
|
||||||
|
* 不覆盖原有请求头并判断是否需要聚合请求头
|
||||||
|
*
|
||||||
|
* @param headerMap 请求头
|
||||||
|
* @param isOverride 是否覆盖
|
||||||
|
* @param isHeaderAggregated 是否聚合
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public HttpConnection header(Map<String, List<String>> headerMap, boolean isOverride, boolean isHeaderAggregated) {
|
||||||
|
if (!isHeaderAggregated){
|
||||||
|
return header(headerMap,isOverride);
|
||||||
|
}
|
||||||
|
if (MapUtil.isNotEmpty(headerMap)) {
|
||||||
|
String name;
|
||||||
|
for (Entry<String, List<String>> entry : headerMap.entrySet()) {
|
||||||
|
name = entry.getKey();
|
||||||
|
List<String> values = entry.getValue();
|
||||||
|
String headValues = StrUtil.join(",", values);
|
||||||
|
this.header(name, StrUtil.nullToEmpty(headValues), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取Http请求头
|
* 获取Http请求头
|
||||||
*
|
*
|
||||||
|
@ -1247,7 +1247,7 @@ public class HttpRequest extends HttpBase<HttpRequest> {
|
|||||||
// issue#3462 自定义body长度
|
// issue#3462 自定义body长度
|
||||||
.setFixedLengthStreamingMode(this.fixedContentLength)
|
.setFixedLengthStreamingMode(this.fixedContentLength)
|
||||||
// 覆盖默认Header
|
// 覆盖默认Header
|
||||||
.header(this.headers, false);
|
.header(this.headers, false, this.isHeaderAggregated);
|
||||||
|
|
||||||
if (null != this.cookie) {
|
if (null != this.cookie) {
|
||||||
// 当用户自定义Cookie时,全局Cookie自动失效
|
// 当用户自定义Cookie时,全局Cookie自动失效
|
||||||
|
@ -270,4 +270,17 @@ public class HttpRequestTest {
|
|||||||
HttpRequest request = HttpRequest.get("http://localhost:9999/qms/bus/qmsBusReportCenterData/getReportDataList?reportProcessNo=A00&goodsName=工业硫酸98%&conReportTypeId=1010100000000000007&measureDateStr=2024-07-01");
|
HttpRequest request = HttpRequest.get("http://localhost:9999/qms/bus/qmsBusReportCenterData/getReportDataList?reportProcessNo=A00&goodsName=工业硫酸98%&conReportTypeId=1010100000000000007&measureDateStr=2024-07-01");
|
||||||
request.execute();
|
request.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHttpHead(){
|
||||||
|
Map<String,String> map = new HashMap<>();
|
||||||
|
map.put("test","test");
|
||||||
|
HttpRequest httpRequest = HttpRequest.post("http://127.0.0.1:8080/testHttpHead")
|
||||||
|
.header("test","test1")
|
||||||
|
.headerMap(map,false)
|
||||||
|
.headerAggregation(true);
|
||||||
|
System.out.println(httpRequest.headers());
|
||||||
|
HttpResponse execute = httpRequest.execute();
|
||||||
|
System.out.println(execute.body());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,81 @@
|
|||||||
|
package cn.hutool.http;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 一个简单的http服务器示例代码;
|
||||||
|
* 用于打印出接收到的请求头和简单的post-json的请求体数据来测试
|
||||||
|
*
|
||||||
|
* @author ZhangWeinan
|
||||||
|
* @date 2025/2/23 15:32
|
||||||
|
*/
|
||||||
|
public class SimpleHttpServer {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
int port = 8080;
|
||||||
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
|
System.out.println("Server started on port " + port);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
new Thread(() -> handleClient(clientSocket)).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void handleClient(Socket clientSocket) {
|
||||||
|
try (BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
OutputStream out = clientSocket.getOutputStream()) {
|
||||||
|
String requestLine = in.readLine();
|
||||||
|
if (requestLine == null) return;
|
||||||
|
|
||||||
|
String[] parts = requestLine.split(" ");
|
||||||
|
String method = parts[0];
|
||||||
|
|
||||||
|
Map<String, String> headers = new HashMap<>();
|
||||||
|
String line;
|
||||||
|
System.out.println("\n=== Headers ===");
|
||||||
|
while (!(line = in.readLine()).isEmpty()) {
|
||||||
|
int colonIndex = line.indexOf(':');
|
||||||
|
if (colonIndex > 0) {
|
||||||
|
String key = line.substring(0, colonIndex).trim();
|
||||||
|
String value = line.substring(colonIndex + 1).trim();
|
||||||
|
headers.put(key, value);
|
||||||
|
System.out.println(key + ": " + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ("POST".equalsIgnoreCase(method)) {
|
||||||
|
int contentLength = Integer.parseInt(headers.getOrDefault("Content-Length", "0"));
|
||||||
|
if (contentLength > 0) {
|
||||||
|
char[] body = new char[contentLength];
|
||||||
|
in.read(body, 0, contentLength);
|
||||||
|
String jsonBody = new String(body);
|
||||||
|
System.out.println("\n=== JSON Body ===");
|
||||||
|
System.out.println(jsonBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String response = "HTTP/1.1 200 OK\r\n" +
|
||||||
|
"Content-Type: text/plain\r\n" +
|
||||||
|
"Content-Length: 16\r\n\r\n" +
|
||||||
|
"Request received";
|
||||||
|
out.write(response.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
clientSocket.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user