博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ip获取请求ip
阅读量:4331 次
发布时间:2019-06-06

本文共 2294 字,大约阅读时间需要 7 分钟。

public class IPAdress    {        public static bool isIP(string str1)        {            if (str1 == null || str1 == string.Empty || str1.Length < 7 || str1.Length > 15) return false;            string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);            return regex.IsMatch(str1);        }        ///         /// 取得客户端真实IP。如果有代理则取第一个非内网地址        ///         public static string fetch()        {            string result = String.Empty;            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];            if (result != null && result != String.Empty) {                if (result.IndexOf(".") == -1)                    result = null;                else {                    if (result.IndexOf(",") != -1) {                        result = result.Replace(" ", "").Replace("'", "");                        string[] temparyip = result.Split(",;".ToCharArray());                        for (int i = 0; i < temparyip.Length; i++) {                            if (isIP(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 (isIP(result))                        return result;                    else                        result = null;                }            }            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;        }    }

 

转载于:https://www.cnblogs.com/jameslif/p/6866090.html

你可能感兴趣的文章
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>