TsgcWebSocketServer_HTTPAPI事件 › OnAfterForwardHTTP

OnAfterForwardHTTP 事件

在 HTTP 请求转发后触发,以便应用程序可以检查结果或上游服务器返回的错误。

语法

property OnAfterForwardHTTP: TsgcWSHTTPAPIAfterForwardHTTP;
// TsgcWSHTTPAPIAfterForwardHTTP = procedure(Connection: TsgcWSConnection; aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse; E: Exception) of object

默认值

备注

OnAfterForwardHTTP 在 OnBeforeForwardHTTP 启用转发且上游 HTTP 请求完成后触发。aRequestInfo 是原始客户端请求,aResponseInfo 是即将写回客户端的响应(ResponseNo、ContentType、ContentText... 根据上游服务器的填充),E 在成功时为 nil,否则为联系目标服务器时引发的异常。使用此事件记录结果,在将响应标头/正文返回给客户端之前重写它们,或在上游调用失败时用自定义错误页面覆盖响应。

示例


procedure OnAfterForwardHTTP(Connection: TsgcWSConnection;
  aRequestInfo: THttpServerRequest; var aResponseInfo: THttpServerResponse;
  E: Exception);
begin
  if Assigned(E) then
  begin
    aResponseInfo.ResponseNo := 502;
    aResponseInfo.ContentText := 'Upstream error: ' + E.Message;
  end
  else
    Log(Format('forwarded %s -> %d', [aRequestInfo.Document, aResponseInfo.ResponseNo]));
end;

返回事件