TsgcWebSocketHTTPServer › 事件 › OnCommandGet
当 HTTP 服务器收到 GET、POST 或 HEAD 请求时触发,以便应用程序构建响应。
property OnCommandGet: TIdHTTPCommandEvent;
// TIdHTTPCommandEvent = procedure(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo) of object
—
OnCommandGet 是组件处理 HTTP 流量(GET、POST 和 HEAD)的主要入口点。ARequestInfo 暴露请求(Document、Params、AuthUsername、Headers、PostStream、RemoteIP 等),AResponseInfo 是传出响应:用正文设置 ContentText 或 ContentStream,用 MIME 类型设置 ContentType,用 HTTP 状态码设置 ResponseNo(200、404 等);调用 AResponseInfo.ServeFile 从磁盘分发文件。处理程序在连接线程的上下文中运行,因此请避免直接访问 UI 或切换到同步分发。已存在于 DocumentRoot 下的文件请求将自动提供服务,不会触发此事件。
procedure OnCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
begin
if ARequestInfo.Document = '/' then
begin
AResponseInfo.ContentText := '<html><head><title>Test Page</title></head><body></body></html>';
AResponseInfo.ContentType := 'text/html';
AResponseInfo.ResponseNo := 200;
end;
end;