我正在开发asp.net网站,现在我的问题是如何使用c#代码获取局域网连接的IP地址,例如打开http://whatismyipaddress.com/显示IP信息:183.82.77.56这样我必须得到IP地址,现在我是这样写的
//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;
}
但是输出错误,请帮助我。
谢谢你