我在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