INDY10的IDHttpServer应答客户端

INDY10的IDHttpServer应答客户端

首先贴源码:

procedure TIdHTTPResponseInfo.WriteContent;
begin
  if not HeaderHasBeenWritten then begin
    WriteHeader;
  end;

  // RLebeau 11/23/2014: Per RFC 2616 Section 4.3:
  //
  // For response messages, whether or not a message-body is included with
  // a message is dependent on both the request method and the response
  // status code (section 6.1.1). All responses to the HEAD request method
  // MUST NOT include a message-body, even though the presence of entity-
  // header fields might lead one to believe they do. All 1xx
  // (informational), 204 (no content), and 304 (not modified) responses
  // MUST NOT include a message-body. All other responses do include a
  // message-body, although it MAY be of zero length.

  if not (
    (FRequestInfo.CommandType = hcHEAD) or
    ((ResponseNo div 100) = 1) or
    (ResponseNo = 204) or
    (ResponseNo = 304)
    ) then
  begin
    // Always check ContentText first
    if ContentText <> '' then begin
      FConnection.IOHandler.Write(ContentText, CharsetToEncoding(CharSet));
    end
    else if Assigned(ContentStream) then begin
      ContentStream.Position := 0;
      FConnection.IOHandler.Write(ContentStream);
    end
    else begin
      FConnection.IOHandler.WriteLn('<HTML><BODY><B>' + IntToStr(ResponseNo) + ' ' + ResponseText    {Do not Localize}
       + '</B></BODY></HTML>', CharsetToEncoding(CharSet));    {Do not Localize}
    end;
  end;

  // Clear All - This signifies that WriteConent has been called.
  ContentText := '';    {Do not Localize}
  ReleaseContentStream;
end; 

一共可以应答客户端3种内容:

1)ContentText不为空字串,则发送字符串

2)ContentStream对象存在,则发送流

3)不是上面2种情况,则发送的是HTML。

最后会自动清空应答的字串或释放流,这也是为什么我们手动释放流反而会报“非法访问”错误:

ContentText := ''; 
ReleaseContentStream;

 

posted @ 2019-01-12 10:17  delphi中间件  阅读(1042)  评论(0编辑  收藏  举报