客户端IP返回::1的解决办法
2008-12-12 11:26
197 查看
刚刚测试程序,一个功能要求获取客户端IP,然而在本机测试过程中发现,客户端(本机)IP始终返回::1,获取 IP 部份的代码如下:
Code
private string GetIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (null == result || result == String.Empty || !IsIP(result))
{
result = "0.0.0.0";
}
return result;
}
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
于是最终获取倒的是因无法匹配而返回的默认IP 0.0.0.0
google了一下,没有结果。
继续想办法。发现不影响获取其它客户端(非本机)IP。根据此现象,我想应该是localhost引起的,于是查看host映射,果然发现::1
(Host映射查看方法 C:\Windows\System32\drivers\etc 文件hosts)
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
::1 localhost
原来是因为操作系统开启了IPV6,它对localhost进行了映射的原因。如果我们需要获取的IP是IPV4的,那么可以删掉::1映射。
如果将来我们需要获取的是IPV6的IP,那么就需要这样的映射。然后程序里统一实现获取IPv6的代码。
Code
private string GetIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (null == result || result == String.Empty || !IsIP(result))
{
result = "0.0.0.0";
}
return result;
}
public static bool IsIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
于是最终获取倒的是因无法匹配而返回的默认IP 0.0.0.0
google了一下,没有结果。
继续想办法。发现不影响获取其它客户端(非本机)IP。根据此现象,我想应该是localhost引起的,于是查看host映射,果然发现::1
(Host映射查看方法 C:\Windows\System32\drivers\etc 文件hosts)
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
::1 localhost
原来是因为操作系统开启了IPV6,它对localhost进行了映射的原因。如果我们需要获取的IP是IPV4的,那么可以删掉::1映射。
如果将来我们需要获取的是IPV6的IP,那么就需要这样的映射。然后程序里统一实现获取IPv6的代码。
相关文章推荐
- “windows 2003域用户在客户端用域服务器IP和域名称访问IIS提示输入用户名密码”解决办法
- 友盟微信,朋友圈分享成功返回app时,退出登录异常解决办法
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- Django 关于POST 返回403 无法连接的解决办法 (亲测)
- 关于SQL语句中SUM函数返回NULL的解决办法
- SSL_connect失败返回-1的一种情况解决办法
- Docker不能绑定静态的外网固定ip的问题及解决办法
- MySQL客户端输出窗口显示中文乱码问题解决办法
- WireShark抓包,may be caused by ip checksum offload的解决办法
- Android请求返回417解决办法
- 系统提示"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本"解决办法
- 服务器双ip部署分布式系统解决办法之一
- ssh客户端无法连接Ubuntu15.04的解决办法
- jmeter http 请求返回socket closed的解决办法
- DHCP之简要分析(包括C语言实现过程,IP冲突的检测与解决办法)
- [Ubuntu网络配置]Ubuntu GNOME版网络配置中无法将默认DHCP方式修改为静态IP的解决办法
- 使用 php 5 时 MySQL 返回乱码的解决办法
- 织梦DEDECMS网站留言板提交时验证码错误返回空白页的解决办法
- mailto 调用邮件客户端乱码问题的解决办法
- apache 服务器不能使用本机IP访问解决办法