C# 获取请求的真实IP,要正确的答案,不然会误导了更多的人,谢谢。
答案:2 悬赏:70 手机版
解决时间 2021-03-17 13:56
- 提问者网友:疯子也有疯子的情调
- 2021-03-17 09:34
C# 获取请求的真实IP,要正确的答案,不然会误导了更多的人,谢谢。
最佳答案
- 五星知识达人网友:零点过十分
- 2021-03-17 10:15
public static string IP
{
get
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if ( result != null && result != String.Empty )
{
//可能有代理
if ( result.IndexOf ( "." ) == -1 ) //没有"."肯定是非IPv4格式
result = null;
else
{
if ( result.IndexOf ( "," ) != -1 )
{
//有",",估计多个代理。取第一个不是内网的IP。
result = result.Replace ( " ", "" ).Replace ( "", "" );
string[] temparyip = result.Split ( ",;".ToCharArray() );
for ( int i = 0; i < temparyip.Length; i++ )
{
if ( IsIPAddress ( temparyip[i] )
&& temparyip[i].Substring ( 0, 3 ) != "10."
&& temparyip[i].Substring ( 0, 7 ) != "192.168"
&& temparyip[i].Substring ( 0, 7 ) != "172.16." )
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if ( IsIPAddress ( result ) ) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}
}
string IpAddress = ( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty ) HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if ( null == result || result == String.Empty )
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if ( result == null || result == String.Empty )
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}直接以变量形式调用IP就行了
{
get
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if ( result != null && result != String.Empty )
{
//可能有代理
if ( result.IndexOf ( "." ) == -1 ) //没有"."肯定是非IPv4格式
result = null;
else
{
if ( result.IndexOf ( "," ) != -1 )
{
//有",",估计多个代理。取第一个不是内网的IP。
result = result.Replace ( " ", "" ).Replace ( "", "" );
string[] temparyip = result.Split ( ",;".ToCharArray() );
for ( int i = 0; i < temparyip.Length; i++ )
{
if ( IsIPAddress ( temparyip[i] )
&& temparyip[i].Substring ( 0, 3 ) != "10."
&& temparyip[i].Substring ( 0, 7 ) != "192.168"
&& temparyip[i].Substring ( 0, 7 ) != "172.16." )
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if ( IsIPAddress ( result ) ) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}
}
string IpAddress = ( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty ) HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if ( null == result || result == String.Empty )
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if ( result == null || result == String.Empty )
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}直接以变量形式调用IP就行了
全部回答
- 1楼网友:拜訪者
- 2021-03-17 11:17
不明白啊 = =!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯