TsgcWebSocketServer_HTTPAPIGebeurtenissen › OnHTTPRequest

OnHTTPRequest Gebeurtenis

Wordt geactiveerd wanneer de server een HTTP-verzoek ontvangt zodat de applicatie het antwoord kan opbouwen.

Syntaxis

__property TsgcWSHTTPAPIRequestEvent OnHTTPRequest;
// typedef void __fastcall (__closure *TsgcWSHTTPAPIRequestEvent)(TsgcWSConnection_HTTPAPI * aConnection, const THttpServerRequest aRequestInfo, THttpServerResponse &aResponseInfo);

Standaardwaarde

Opmerkingen

OnHTTPRequest is het belangrijkste toegangspunt voor elk HTTP-verzoek dat HTTP.SYS aan de server levert (GET, POST, PUT, DELETE, HEAD...). aRequestInfo stelt het gedecodeerde verzoek beschikbaar (Document, Method, Headers, ContentType, Content, QueryParams, Cookies, ContentLength, AuthExists/AuthUsername/AuthPassword, Stream) en aResponseInfo verzamelt het antwoord dat de toepassing wil terugsturen: stel ResponseNo, ContentText (of ContentStream / FileName voor binaire/bestandsantwoorden), ContentType, CustomHeaders, Date/Expires/LastModified en CacheControl naar behoefte in. Laat aResponseInfo ongewijzigd om de standaard HTTP 200 te retourneren, of wijs ResponseNo toe aan 404/500/... om foutcondities te signaleren. Deze gebeurtenis wordt niet geactiveerd voor WebSocket-upgradeaanvragen; die worden gerouteerd via OnConnect/OnHandshake.

Voorbeeld


void __fastcall OnHTTPRequest(TsgcWSConnection_HTTPAPI *aConnection,
  const THttpServerRequest *aRequestInfo,
  THttpServerResponse *aResponseInfo)
{
  if (aRequestInfo->Method == "GET")
  {
    if (aRequestInfo->Document == "/test.html")
    {
      aResponseInfo->ResponseNo = 200;
      aResponseInfo->ContentText = "OK";
      aResponseInfo->ContentType = "text/html; charset=UTF-8";
    }
    else
      aResponseInfo->ResponseNo = 404;
  }
  else
    aResponseInfo->ResponseNo = 500;
}

Terug naar Events