我在Silverlight中使用ASP.NET(.asmx)Web服务。由于无法在Silverlight中查找客户端IP地址,因此我必须在服务端记录此信息。 这些都是一些方法我都试过:ASP.NET中的客户端IP地址(.asmx)webservices
Request.ServerVariables("REMOTE_HOST")
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Request.UserHostAddress()
Request.UserHostName()
string strHostName = Dns.GetHostName();
string clientIPAddress = Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
上述所有方法我的本地系统上正常工作,但是当我发布一个生产服务器上我的服务,它开始给错误,
Error: Object reference not set to an instance of an object. StackTrace:
at System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(Int32 index)
at System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name)
at System.Web.Hosting.ISAPIWorkerRequest.GetRemoteAddress()
at System.Web.HttpRequest.get_UserHostAddress()
空引用异常或HttpContext.Current.Request.ServerVariables [“REMOTE_ADDR”] 我只是不能找出任何方式获取客户端IP在我的ASMX服务。 =( – 2010-03-29 19:52:04
@ Zain:就像我说的,在使用任何这些值之前检查null。实际上,在尝试'HttpContext.Current.Request'之前,一定要测试'HttpContext.Current'来查看它是否为null。 – 2010-03-29 20:50:31