使用IP地址查找访问者的位置 [英] find visitor's location using IP address
问题描述
朋友们,
我在找到访客的位置时遇到一些问题.我的代码可能会产生一些错误.我已经在下面粘贴了我的代码
代码:
Hi friends,
I have some problem to finding visitor''s location.my code could be produce some error. i have pasted my code below
Code:
string ipaddress;
System.Web.HttpRequest Request;
ipaddress =Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
DataTable dt = GetLocation(ipaddress);
if (dt != null)
{
if (dt.Rows.Count > 0)
{
city = dt.Rows[0]["City"].ToString();
RegionName = dt.Rows[0]["RegionName"].ToString();
CountryName = dt.Rows[0]["CountryName"].ToString();
CountryCode = dt.Rows[0]["CountryCode"].ToString();
}
else
{
}
}
错误行:
ipaddress = Request.ServerVariables ["HTTP_X_FORWARDED_FOR"];
错误消息:
使用未分配的局部变量" Request"
任何人都知道可以解决此问题并使用ip地址获取访问者的位置.
问候
sasi
Error Line:
ipaddress =Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Error message:
"Use of unassigned local variable ''Request'' "
anybody knows to solve this issue and get the visitors location using ip address.
regards
sasi
推荐答案
这是我一生中第一次看到Request被定义为局部变量.您最好先阅读并了解它.
http://msdn.microsoft.com/en-us/library/system.web. httprequest.aspx
无论如何,一旦您了解了它的工作原理,就应该能够自己修复它.如果您仍然有疑问,请回到这里,肯定会有人帮助您.祝你好运.
This is the first time in my life I have seen Request being defined as local variable. You better read and understand about it first.
http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx
Anyways, you should be able to fix it yourself once you understand how it works. If you still have doubts, get back here and someone will surely help you. Good Luck.
System.Web.HttpRequest Request;
ipaddress =Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
您已经定义了Request
,但尚未对其进行初始化,因此未定义消息.
You have defined Request
but you have not initialized it, hence your "unassigned variable" message.
这篇关于使用IP地址查找访问者的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!