获取请求客户端的 IP 地址(Soap 消息)
我构建了一个 ASMX 服务,并在代码中处理来自 ELMAH 的错误日志。 我将所有字段添加到数据库中,然后添加更多字段,其中一个是所请求客户端的 IP 地址...
但我无法(请阅读,不知道如何)得到它...
我习惯
string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == "") ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
但当然这里不适用,因为它不是 HTML 请求,而是
ALL_HTTP 标头中的 SOAP 请求 我得到所有这些信息:
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:440331
HTTP_CONTENT_TYPE:text/xml; charset=utf-8
HTTP_EXPECT:100-continue
HTTP_HOST:www.mydomainname.com
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3074)
HTTP_VSDEBUGGERCAUSALITYDATA:uIDPowJ7s0UYlmhEqcDjxYQT/TQAAAAADlaOOkJ5DUizA7gKLg3YEPur8pFWlaVFmpinSAOBTuIACQAA
HTTP_SOAPACTION:"http://www. mydomainname.com/webservices/ErrorReportService/ReportErrors"
在哪里以及如何获取请求的 IP?
请注意,我还没有使用任何类型的身份验证......:)
I builded a ASMX Service and in the code that process an Error Log that comes from ELMAH.
I add into the database all the fields and I add some more, one is the IP Address of the requested client...
But I can't (please read, don't know how to) get that...
I'm used to
string ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == "") ipAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
But off course here it does not apply because it's not an HTML request but a SOAP request
in ALL_HTTP header I get all this information:
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:440331
HTTP_CONTENT_TYPE:text/xml; charset=utf-8
HTTP_EXPECT:100-continue
HTTP_HOST:www.mydomainname.com
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3074)
HTTP_VSDEBUGGERCAUSALITYDATA:uIDPowJ7s0UYlmhEqcDjxYQT/TQAAAAADlaOOkJ5DUizA7gKLg3YEPur8pFWlaVFmpinSAOBTuIACQAA
HTTP_SOAPACTION:"http://www. mydomainname.com/webservices/ErrorReportService/ReportErrors"
Where and how can I get the Requested IP?
Note that I'm not using any kind of authentication (yet) ... :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够使用 HttpContext.Current.Request.UserHostAddress,但如果请求计算机位于代理服务器或 NAT 设备后面,则这将不可靠。 您将获得代理服务器的地址。
我想澄清一下 - IP 地址并不是一个合适的唯一标识符。 它不识别计算机。 最好的情况是,它标识一台特定计算机上的一个特定网络接口在某一特定时间使用的网络层地址。 在不同的时间,同一网络接口可能使用不同的 IP 地址。 在不同的时间点,同一台计算机可能有多个网络接口。 你就是不知道。
我好像记得读过,TCP/IP协议的设计者特意明确表示IP地址不能作为计算机的唯一标识符。 不幸的是,读过这篇文章的人似乎太少了。
You should be able to use HttpContext.Current.Request.UserHostAddress, but this will not be reliable if the requesting machine is behind a proxy server or NAT device. You'll get the address of the proxy server instead.
I want to make it clear - an IP address is not an appropriate unique identifier. It does not identify a computer. At best, it identifies the Network Layer address that one particular network interface on one particular computer is using at one particular time. At a different time, that same network interface could be using a different IP address. At a different point in time, that same computer might have more than one network interface. You just can't tell.
I seem to remember reading that the designers of the TCP/IP protocols deliberately made it clear that an IP address cannot be used as a unique identifier of a computer. Unfortunately, it seems too few people have read that.