gpt4 book ai didi

http - Netty - 访问分块 HTTP 请求的内容 (LittleProxy)

转载 作者:可可西里 更新时间:2023-11-01 17:02:55 30 4
gpt4 key购买 nike

我正在用 LittleProxy 拦截一个 HTTP 请求它使用 Netty。但是,现在我想拦截一个明显使用分块传输编码的网络服务请求。

HTTP header 如下所示

Content-Type -> text/xml; charset=UTF-8
Host -> 192.168.56.1:7897
SOAPAction -> "getSymbols"
Transfer-Encoding -> chunked
User-Agent -> Axis2
Via -> 1.1.tvmbp

如何访问内容?我试过将 httpChunkAggregator 添加到 littleproxy 代码中的某个管道,但没有用。

最佳答案

您需要在 HttpFiltersSourceAdapter 中覆盖这两个方法。返回一个非零缓冲区大小。 LittleProxy 将自动聚合 httpRequest 和 httpContent 并包装到 AggregatedFullHttpRequest 中,允许转换为 httpContent。

@Override
public int getMaximumRequestBufferSizeInBytes() {
return 1024 * 1024;
}

@Override
public int getMaximumResponseBufferSizeInBytes() {
return 1024 * 1024 * 2;
}

然后就可以克隆并读取HTTP包中的内容了:

String cloneAndExtractContent(HttpObject httpObject, Charset charset){
List<Byte> bytes = new ArrayList<Byte>();
HttpContent httpContent = (HttpContent) httpObject;
ByteBuf buf = httpContent.content();
byte[] buffer = new byte[buf.readableBytes()];
if(buf.readableBytes() > 0) {
int readerIndex = buf.readerIndex();
buf.getBytes(readerIndex, buffer);
}
for(byte b : buffer){
bytes.add(b);
}
return new String(Bytes.toArray(bytes), charset);
}


@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
System.out.println("clientToProxyRequest - to -> "+getRequestUrl());
System.out.println(cloneAndExtractContent(httpObject, StandardCharsets.UTF_8));

return null;
}


@Override
public HttpObject serverToProxyResponse(HttpObject httpObject)
{
System.out.println("serverToProxyResponse <- from - "+getRequestUrl());
System.out.println(cloneAndExtractContent(httpObject, StandardCharsets.UTF_8));

return httpObject;
}

关于http - Netty - 访问分块 HTTP 请求的内容 (LittleProxy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610862/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com