TsgcWebSocketServer_HTTPAPIEvents › OnHTTPRequest

OnHTTPRequest 事件

当服务器收到 HTTP 请求时触发,以便应用程序可以构建响应。

语法

property OnHTTPRequest: TsgcWSHTTPAPIRequestEvent;
// TsgcWSHTTPAPIRequestEvent = procedure(aConnection: TsgcWSConnection_HTTPAPI; const aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse) of object

默认值

备注

OnHTTPRequest 是 HTTP.SYS 传递给服务器的每个 HTTP 请求(GET、POST、PUT、DELETE、HEAD 等)的主要入口点。aRequestInfo 提供已解码的请求信息(Document、Method、Headers、ContentType、Content、QueryParams、Cookies、ContentLength、AuthExists/AuthUsername/AuthPassword、Stream),aResponseInfo 用于收集应用程序希望发送回的响应:根据需要设置 ResponseNo、ContentText(或 ContentStream/FileName 用于二进制/文件响应)、ContentType、CustomHeaders、Date/Expires/LastModified 和 CacheControl。保留 aResponseInfo 不变则返回默认的 HTTP 200,将 ResponseNo 设为 404/500/... 以表示错误条件。此事件不会针对 WebSocket 升级请求触发;这些请求会通过 OnConnect/OnHandshake 路由处理。

示例


procedure OnHTTPRequest(aConnection: TsgcWSConnection_HTTPAPI;
  const aRequestInfo: THttpServerRequest;
  var aResponseInfo: THttpServerResponse);
begin
  if aRequestInfo.Method = 'GET' then
  begin
    if aRequestInfo.Document = '/test.html' then
    begin
      aResponseInfo.ResponseNo := 200;
      aResponseInfo.ContentText := 'OK';
      aResponseInfo.ContentType := 'text/html; charset=UTF-8';
    end
    else
      aResponseInfo.ResponseNo := 404;
  end
  else
    aResponseInfo.ResponseNo := 500;
end;

返回事件