TWebRequest
有两个方法用于检索客户端 IP 地址:
- 远程IP
- 远程地址
来自docs of RemoteIP :
Specifies the IP of the remote target machine associated with the HTTP request message. Read the RemoteIP property to obtain the IP address of the remote target machine associated with the HTTP request message.
Indicates the remote IP address of the client associated with the HTTP request message. Read RemoteAddr to obtain the IP address of the source of the Web client request.
它们看起来非常接近,但 RemoteIP 返回一个空字符串。 我想检索客户端IP地址,正确的方法是什么?
最佳答案
如果我们查看源代码,我们会在 TWebRequest 的声明中发现以下内容
property RemoteIP: string read GetRemoteIP;
property RemoteAddr: string index 21 read GetStringVariable;
实现:
function TWebRequest.GetRemoteIP: string;
begin
Result := EmptyStr;
end;
而 GetStringVariable 是一个虚拟方法。
让我们看一下子类之一 - TISAPIRequest,我们会发现以下内容:
LResult := GetFieldByNameA(ServerVariables[Index]);
ServerVariables[21] 是“REMOTE_ADDR” header ,显示客户端或代理服务器的 IP。
但是 GetRemoteIP 没有实现。
请求中似乎没有使用 RemoteIP,因为 TCGIRequest、TApacheRequest 和 TWinCGIRequest 也没有实现 GetRemoteIP。
关于Delphi Web.HTTPApp.TWebRequest RemoteIP VS RemoteAddr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920491/