Delphi XE5 Rest Datasnap Server. Getting Client IP Address

元气小坏坏 提交于 2019-12-11 01:47:31

问题


I'm trying to get a client's IP address and other client information using DSServer's onconnect event with the following code.

My problem is that DSConnectEventObject.ChannelInfo is nil every time. Additionally, I can't resolve the IP addresses.

Please help me. Thank you.

procedure TWebModule1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
var
     ci: TDBXClientInfo;
begin
     ci := DSConnectEventObject.ChannelInfo.ClientInfo;
     AddLog(Format('Client %s Connected IP: %s, Port: %s', 
         [ci.Protocol, ci.IpAddress, ci.ClientPort])
     );
end;

回答1:


As mentioned previously, this is a bug in DataSnap. It was working fine in XE2 but the error came in somewhere between XE3 and XE5. It has been reported in QC reports #121931 and #126164. Luckily, the client connection properties are available in the Session object - see below :

var
  Session: TDSSession;
  Protocol, IpAddress, AppName: string;
begin
  Session := TDSSessionManager.GetThreadSession;
  Protocol := Session.GetData('CommunicationProtocol');
  IpAddress := Session.GetData('RemoteIP');
  AppName := Session.GetData('RemoteAppName');
end;



回答2:


DataSnap REST ISAPI dll


in WebMoudle unit,

save IP:

TDSSessionManager.GetThreadSession.PutData('RemoteAddr',Request.RemoteAddr);


in other unit,

get IP:

uIP := TDSSessionManager.GetThreadSession.GetData('RemoteAddr');





回答3:


how about this?

in WebMoudle unit,

save IP:

implementation
threadvar remoteIP: string;

procedure TWebModule1.WebModuleBeforeDispatch(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  remoteIP := Request.RemoteAddr;
end;

procedure TWebModule1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject);
begin
  TDSSessionManager.GetThreadSession.PutData('RemoteAddr', remoteIP);
end;

procedure TWebModule1.DSServer1Disconnect(DSConnectEventObject: TDSConnectEventObject);
begin
  remoteIP := '';
end;



回答4:


This is Bug.
You can do it code, below:

procedure TWebModule.DSServerConnect(
  DSConnectEventObject: TDSConnectEventObject);
var _Session: TDSSession;
begin
  try
    if Assigned(DSConnectEventObject.ChannelInfo) then
    begin
      _Session := TDSSessionManager.GetThreadSession;
      if Assigned(_Session) then
      begin
        if _Session.GetData('RemoteAddr') = '' then
          _Session.PutData('RemoteAddr', DSConnectEventObject.ChannelInfo.Info);
      end;
    end;
  except
  end;
end;


来源:https://stackoverflow.com/questions/19299308/delphi-xe5-rest-datasnap-server-getting-client-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!