gpt4 book ai didi

Delphi REST 服务器,使用压缩的 Android 客户端

转载 作者:行者123 更新时间:2023-12-05 04:18:26 35 4
gpt4 key购买 nike

我们正在构建一个 Delphi REST 服务器,它为原生 Android 应用程序提供相当大的数据 block (每个请求 1.5MB,其中有很多)。一切正常,除了这种情况下的数据大小会出现问题,导致我们环境中的传输时间过长(移动数据速率有限)。我已经尝试在 DSHTTPWebDispatcher 上添加 ZLibCompression 过滤器,但响应仅作为未压缩的文本/html 再次返回。

有什么方法可以强制服务器使用在发送前作为事件添加的过滤器吗?

服务器使用 Delphi XE3 构建。

最佳答案

我已经设法找出在 DataSnap 项目中添加压缩和相关 header 更改的位置。

这里的关键是 TWebModule 类。如果使用向导创建一个新项目,将构造 TWebModule 类的默认实现,具有 BeforeDispatch、AfterDispatch 等事件属性。这里的命名是指将传入请求分派(dispatch)到将要处理的地方的 Action 。因此,BeforeDispatch 在请求到达时发生,一些处理在服务器上发生,AfterDispatch 在响应发送回调用者之前触发。

因此,如果想在事后修改构造的响应,则 AfterDispatch 是正确的事件。这可能包括对内容和标题的更改。

关于 AfterDispatch 事件:

procedure TWebModule1.WebModuleAfterDispatch(
Sender: TObject;
Request: TWebRequest;
Response: TWebResponse;
var Handled: Boolean);
var

srcbuf, destbuf : TBytes;
str : string;

begin
str := Response.Content;

//prepare byte array
srcbuf := BytesOf(str);

//compress to buff (System.ZLib)
ZCompress(srcbuf, destbuf, zcMax);

//prepare responsestream and set content encoding and type
Response.Content := '';
Response.ContentStream := TMemoryStream.Create;
Response.ContentEncoding := 'deflate';
Response.ContentType := 'application/json';

//current browser implementations incorrectly handles the first 2 bytes
//of a ZLib compressed stream, remove them
Response.ContentStream.Write(@(destbuf[2]),length(destbuf)-2);
Response.ContentLength := (length(destbuf))-2;
end;

不是很花哨,可以根据发回的内容启用/禁用压缩,但对于我们的实现,我们保持简单。

这 100% 与 Fiddler 和可以处理 deflate 的浏览器一起工作。

关于Delphi REST 服务器,使用压缩的 Android 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14577344/

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