You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Delphi独立Web服务器中获取客户端IP地址的方法

在Delphi的TIdHTTPWebBrokerBridge中获取客户端IP地址

在使用TIdHTTPWebBrokerBridge搭建的独立Web服务器中,有两种可靠的方式获取客户端IP:

  • 方式一:通过WebBroker的TWebRequest对象获取
    在WebModule的请求处理Action中,直接使用Request.RemoteAddr属性即可拿到客户端IP,这是WebBroker框架提供的标准方法,使用简单且兼容性好。

    示例代码:

    procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
      Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
    begin
      Response.Content := '当前客户端IP:' + Request.RemoteAddr;
    end;
    
  • 方式二:通过Indy底层的TIdContext对象获取
    利用TIdHTTPWebBrokerBridge的OnCommandGet(或OnCommandOther等请求事件),从参数中的AContext对象获取底层连接信息,通过AContext.Binding.PeerIP拿到客户端IP,适合需要更原始连接数据的场景。

    示例代码:

    procedure TForm1.IdHTTPWebBrokerBridge1CommandGet(AContext: TIdContext;
      ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
    begin
      AResponseInfo.ContentText := '客户端IP:' + AContext.Binding.PeerIP;
    end;
    

注意:如果服务器部署在反向代理(如Nginx)之后,上述方法拿到的可能是代理服务器的IP,此时需要读取代理传递的X-Forwarded-For等请求头字段来获取真实客户端IP。

内容的提问来源于stack exchange,提问作者Ham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:57:03