TsgcWebSocketServer_HTTPAPI | Send Text Response
HTTP 요청을 처리하려면 OnHTTPRequest 이벤트를 사용하십시오.
THttpServerRequest 클래스는 HTTP Request 데이터를 포함합니다.
- Document: 피어가 액세스하려는 Document입니다.
- Method: HTTP 메서드('GET', 'POST' 등)
- Headers: HTTP 요청의 Headers.
- AcceptEncoding: accept encoding 변수, 예: "gzip, deflate, br".
- ContentType: 예: "text/html"
- Content: 존재하는 경우 요청의 콘텐츠입니다.
- QueryParams: 쿼리 매개변수입니다.
- Cookies: 존재하는 경우 쿠키입니다.
- ContentLength: 콘텐츠의 크기입니다.
- AuthExists, AuthUsername, AuthPassword: 인증 요청 데이터입니다.
- Stream: http 요청에 본문이 있는 경우, 이것은 본문의 스트림입니다.
THttpServerResponse 클래스는 HTTP 응답 데이터를 포함합니다.
- ContentText: 텍스트 형태의 응답입니다.
- ContentType: 예: "text/html". ContentText를 UTF8로 인코딩하려면 charset='utf-8'을 설정하십시오. 예: text/html; charset=utf-8
- CustomHeaders: 자체 헤더를 보내야 하는 경우 이 변수를 사용하십시오
- AuthRealm: 서버가 인증을 요구하면 이 변수를 설정하십시오.
- ResponseNo: HTTP 응답 번호입니다. 예: 200은 응답이 성공적임을 의미합니다.
- ContentStream: 응답에 스트림이 포함된 경우 여기에 설정하십시오(스트림을 해제하지 마십시오. 자동으로 해제됩니다).
- FileName: 응답이 파일 이름인 경우 여기에 파일 이름의 전체 경로를 설정하십시오.
- Date, Expires, LastModified: 응답의 datetime 변수입니다.
- CacheControl: 캐시 동작을 사용자 지정할 수 있습니다.
예제: 서버가 "/test.html" 문서에 대한 GET 요청을 받으면 OK 응답을 보내고, 그렇지 않으면 다른 문서에 대한 GET 요청인 경우 404를, 다른 method인 경우 오류 500을 보냅니다.
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;