首页 > 解决方案 > Indy http 服务器会话不能正常工作

问题描述

如何创建与 idhttpserver 一起使用的会话?

我尝试了很多方法,但我无法在 ARequestInfo.Session 或 AResponseInfo.Session 中访问会话对象,它们都始终为零。请帮忙

procedure TFolsecPermissionManager.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  Command: TCommand;
  Session: TIdHTTPSession;
begin
  Session := IdHTTPServer.CreateSession(AContext, AResponseInfo, ARequestInfo);
  IdHTTPServer.SessionList.Add(Session);


  Command:= TCommand.Create;
  Command.Start(AContext, ARequestInfo, AResponseInfo);
end;

标签: delphiindyhttpserverindy10

解决方案


确保TIdHTTPServer.SessionState设置为True. 或者,您也可以设置TIdHTTPServer.AutoStartSessionTrue。默认情况下,它们都是False

如果两者都是True,则不需要CreateSession()手动调用,因为它会自动调用每个不携带现有会话 cookie 的传入请求。

如果SessionState=TrueAutoStartSession=False,您确实需要在需要CreateSession()时手动调用。

但是,无论如何,不​​要SessionList.Add()手动CreateSession()调用,因为在内部为您调用。您不希望SessionList持有对同一TIdHTTPSession对象的多个引用。


推荐阅读