HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了简单的IPHelper.cs 访客IP获取类大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
public class IPHelper
{
    public static String GetVisitorsIpaddress()
    {
        String result = String.Empty;
 
        result = System.Web.httpContext.Current.request.ServerVariables["http_X_FORWARDED_FOR"];
 
        @H_403_35@//@H_403_35@ 如果使用代理,获取真实IP
        if (result != null && result.IndexOf(".") == -1)    @H_403_35@//@H_403_35@没有“.”肯定是非IPv4格式
            result = null;
        else if (result != null)
        {
            if (result.IndexOf(",") != -1)
            {
                @H_403_35@//@H_403_35@有“,”,估计多个代理。取第一个不是内网的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];    @H_403_35@//@H_403_35@找到不是内网的地址
                    }
                }
            }
            else if (IsIpaddress(result)) @H_403_35@//@H_403_35@代理即是IP格式
                return result;
            else
                result = null;    @H_403_35@//@H_403_35@代理中的内容 非IP,取IP
        }
        if (null == result || result == String.Empty)
            result = System.Web.httpContext.Current.request.ServerVariables["REMOTE_ADDR"];
 
        if (result == null || result == String.Empty)
            result = System.Web.httpContext.Current.request.UserHostAddress;
 
        return result;
    }
 
    /// <sumMary>
    ///@H_403_35@ 判断是否是IP地址格式 0.0.0.0
    /// </sumMary>
    /// <param name="str1">@H_403_35@待判断的IP地址</param>
    /// <returns>@H_403_35@true or false</returns>
    private static bool IsIpaddress(String str1)
    {
        if (str1 == null || str1 == String.Empty || str1.Length < 7 || str1.Length > 15) return false;
 
        String regformat = @"^\d{1,3}[\.]\d{1,3}$";
 
        Regex regex = new Regex(regformat,RegexOptions.IgnoreCasE);
        return regex.Ismatch(str1);
    }
}

大佬总结

以上是大佬教程为你收集整理的简单的IPHelper.cs 访客IP获取类全部内容,希望文章能够帮你解决简单的IPHelper.cs 访客IP获取类所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。