C#获取IP地址

一、获取客户端IP

C#获取IP地址
        public 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)
            {
                return "0.0.0.0";
            }
            return result;
        } 
客户端IP[适用于Web]

二、获取服务端IP

C#获取IP地址
 private string  GetIP()
        {
            string StrIp="";

            System.Net.IPAddress[] AddressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;
            for (int i = 0; i < AddressList.Length; i++)
            {
                StrIp += AddressList[i].ToString() + "\n";
            }
                return StrIp;
        }
服务端IP[适用于窗体] 
原文引用地址:https://www.iteye.com/blog/cpine-474163

C#获取IP地址

上一篇:在win10修改jupyter notebook(Anaconda安装)工作路径


下一篇:转:关于ASP.NET中WEBAPI中POST请求中FromBody修饰的string类型的参数服务器端获取不到值FromBody空值的简单解决方法