- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当 TIdContext.Connection 触发 OnWorkEnd 事件时,可以从 TIdTCPConnection 检索一些信息(用于记录目的)吗?
我想要以下信息: - 用户ip地址(在Socket.Binding.PeerIP中找到我自己) - 浏览器/客户端用户代理 - 日期时间开始请求 - 请求的总大小 - 字节发送 - 发送文件的文件名
我的服务器非常简单,对每个请求,都使用文件流进行响应。
procedure TMyHttpServer.OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
AResponseInfo.ContentStream := TFileStream.Create('C:\server\file.exe', fmOpenRead or fmShareDenyNone);
AContext.Connection.OnWorkEnd := MyOnWorkEnd;
end;
procedure TMyHttpServer.MyOnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
var
aConnection : TIdTCPConnection;
aIPAddress, aFileName, aDateStart, aByteSend, aFileSize, aUserAgent : string;
aDateEnd : TDateTime;
begin
aConnection := TIdTCPConnection(ASender);
aIPAddress := aConnection.Socket.Binding.PeerIP;
aFileName := ''; // Filename download
aDateStart := ''; // Date start download
aDateEnd := Now;
aByteSend := ''; // byte send
aFileSize := ''; // file size
aUserAgent := ''; // user agent
WriteLog(aFileName + ' ' + aDateStart +' '+aDateEnd +' etc.');
end;
最佳答案
无法在 OnWork...
事件中直接访问请求和响应信息。您必须手动传递信息。我建议:
从 TFileStream
派生一个新类来存储所需的信息,然后在服务器响应后释放 ContentStream
时在该类的析构函数中处理该信息传输完成。
从 TIdServerContext
派生一个新类,以保存指向 TIdHTTPRequestInfo
和 TIdHTTPResponseInfo
对象的指针:
type
TMyContext = class(TIdServerContext)
public
Request: TIdHTTPRequestInfo;
Response: TIdHTTPResponseInfo;
end;
然后,您可以在激活服务器之前将该类类型分配给服务器的 ContextClass
属性,并在 OnCommandGet
事件中对 AContext
参数进行类型转换到您的类类型,以便您可以分配其指针,并将 AContext
对象分配给 AContext.Connection.Tag
属性:
MyHttpServer.ContextClass := TMyContext;
...
procedure TMyHttpServer.OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
TMyContext(AContext).Request := ARequestInfo;
TMyContext(AContext).Response := AResponseInfo;
AContext.Connection.Tag := NativeInt(AContext);
//...
end;
在 OnWork...
事件中,您可以对 Sender
参数进行类型转换以到达其 Tag
,然后进行类型转换到您的自定义类以到达其存储的请求/响应指针:
procedure TMyHttpServer.MyOnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
var
aConnection : TIdTCPConnection;
aContext: TMyContext;
//...
begin
aConnection := TIdTCPConnection(ASender);
aContext := TMyClass(aConnection.Tag);
//...
end;
#2 的一个细微变化是操纵 OnWorkEnd
事件处理程序的 Self
指针来传递 Context
对象直接连接到处理程序,而不使用 Connection.Tag
属性:
type
TMyContext = class(TIdServerContext)
public
Request: TIdHTTPRequestInfo;
Response: TIdHTTPResponseInfo;
MyServer: TMyHttpServer;
end;
...
procedure TMyHttpServer.OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Handler: TWorkEndEvent;
begin
TMyContext(AContext).Request := ARequestInfo;
TMyContext(AContext).Response := AResponseInfo;
TMyContext(AContext).MyServer := Self;
Handler := MyOnWorkEnd;
TMethod(Handler).Data := TMyContext(AContext);
AContext.Connection.OnWorkEnd := Handler
//...
end;
procedure TMyHttpServer.MyOnWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
var
aConnection : TIdTCPConnection;
aContext: TMyContext;
aServer: TMyHttpServer;
//...
begin
aConnection := TIdTCPConnection(ASender);
aContext := TMyClass(Self);
aServer := aContext. MyServer;
//...
end;
关于德尔福 IdHTTPServer (Indy 10.6) : retrive some request/response info from TIdTCPConnection in OnWorkEnd event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196637/
我尝试使用SHA-256加密功能,但没有成功。 我需要获取字符串和文件的哈希值。我使用 Delphi 10.1 Berlin 和 Indy for Hash。 我的代码: uses System.Cl
是否有关于如何在 Lazarus 中安装 Indy 10 组件的分步指南? 我在 https://svn.atozed.com:444/svn/Indy10 查看了最后一个 snv 快照 用户名:In
我在 Delphi 7 中编写了一个程序(包括一个使用 Indy 的 ModBus 组件)。在我的机器上,它使用 Indy 9 并且工作正常。它通过 ModBus 协议(protocol)与其他机器进
我是 indy 新手,使用 CBuilder XE4 附带的任何版本。这是非常简单的代码,在我正在阅读的内容超过 16K 限制之前都可以正常工作...... String Ttcp_mgr::send
我在 4 个基于 ubuntu 的机器上设置了 4 个节点。我使用以下命令生成了这些节点所需的 key :init_indy_node VAL1 0.0.0.0 9701 0.0.0.0 9702 1
当前应用程序:Delphi 2006、indy9(大量使用 SOAP 和其他组件) 需要转到 Indy 10 以获得 TLS 支持(poodle 等)。 看着 http://www.indyproje
我是 C++ Builder 的新手。三天前,我安装了 Embarcadero®。 C++Builder® 2010。这门语言对我来说非常有趣。 在 Delphi 中,我通常使用 Indy 9 和 1
我对 SSL 和这些加密的东西完全陌生,但我需要让在移动设备上运行的客户端和服务器 Delphi XE6 应用程序进行通信。必须对 TCP 通信进行安全加密。 首先,我简单地编写了基于 Win32 客
我有三个问题: 是否可以通过多个连接破坏IdTCPServer? 我尝试测试我的应用程序,并且当我有多个连接时 - 它工作得很好(甚至几天),但是当有时连接数增加时,应用程序会导致访问冲突。我写的应用
我使用 Delphi XE 5/6,默认 Indy 版本,Windows 7。IdMappedPortTCP(放在表单上的组件)将本地流从 FFmpeg 重定向到 Twitch.tv,并且总是接近 2
我有一个客户端应用程序,它应该向可选的服务器应用程序发送通知消息。客户端不应受到服务器应用程序是否存在的影响。它应该尝试连接到服务器应用程序并发送通知消息,如果出现错误,它应该默默地忽略所有错误并继续
我在使用Indy(idTCPServer组件)读取客户端发送的数据时遇到一些问题,数据本身是十六进制格式的,所以我不能使用AThread.Connection.ReadLn( );为此... 这是我的
当Indy收到404时,我无法获取响应内容,它总是空的。 例如使用Indy访问https://www.etsy.com/people/car ,我收到 404 并且内容流(或分配的流)只是空的。对TH
很久以前,我使用 Delphi 2007 和 WebBroker (TWebModule) 启动了一些“Web 应用程序”。当时我假设您需要一个 Web 服务器(例如 Apache 或 Microso
这是我的程序: Application.FTP.Host:= vHost; Application.FTP.Port:= vPort; try Application.FTP.AutoLogin
以下代码请求 OAuth2 token 。它适用于 Indy 10.0.52,但对于 Indy 10 SVN 5412,由于请求凭据无效,它会生成 400 Bad Request 错误。 WXYZUs
我有一个应用程序需要从网络驱动器进行一些 FTP 上传。我为此使用 Indy。然后,当文件位于网络驱动器上并成功上传到 FTP 服务器时,我想通过电子邮件将同一文件发送给同事。 我正在使用下面的代码来
我正在使用 Indy UDP Server 读取一串数据。但是,我真的不知道如何使用 Adata 参数。我可以通过使用下面的 ByteToBin 函数将其转换为二进制,然后使用 BintoHex1 将
我是独立服务器的新手,所以我正在努力完成这个简单的任务。我必须创建一个服务器并上传一个小文件;它的大小始终为 128 字节。然后,当有人打开服务器主页时,文件会自动发送。所以: 在磁盘上上传一个文件(
我正在使用 Delphi 10.0 西雅图。 我想向 UDP 服务器发送请求,然后读取服务器响应,这是一个简单的字符串: Client side:send('12345') server side(o
我是一名优秀的程序员,十分优秀!