0
我正在开发asp.net网站,现在我的问题是如何获取lan使用c#代码连接ip地址,例如打开http://whatismyipaddress.com/显示IP信息:183.82.77.56这样我就得到了ip地址,现在我是像这样写如何使用asp.net获取网络ip地址(Internet)?
//Get Lan Connected IP address method
public string GetLanIPAddress()
{
//Get the Host Name
string stringHostName = Dns.GetHostName();
//Get The Ip Host Entry
IPAddress[] arrIpAddress1 = Dns.GetHostAddresses(stringHostName);
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;
return arrIpAddress[arrIpAddress.Length - 1].ToString();
}
//Get Visitor IP address method
public string GetVisitorIpAddress()
{
string stringIpAddress;
stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
{
stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
string add = HttpContext.Current.Request.UserHostAddress;
}
return "Your ip is " + stringIpAddress;
}
但错误的输出,请帮助我的任何一个。
感谢ü hemanth
在那里我可以添加此代码,请给我一个例子 – hmk 2012-08-14 06:06:39
低于此line IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName); – 2012-08-14 06:08:35
AddressList count count为4,1为{fe80 :: c90d:59f2:6d65:8100%11},2为{fe80 :: 18d7:2872:3f57:fe35%12},3为{192.168.1.202} (我的电脑IP),4是{2001:0:9d38:953c:18d7:2872:3f57:fe35}但我有183.82.77.56(这是我的带宽IP) – hmk 2012-08-14 06:12:36