码迷,mamicode.com
首页 > 其他好文 > 详细

获取客户端IP 和 外网IP

时间:2018-07-04 13:48:14      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:ethos   space   就是   generic   gen   .net   for   end   数据   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Common
{
public class CheckIP
{
#region 判断是否是IP格式

public static bool IsIPAddress(string strIP)
{
if (string.IsNullOrEmpty(strIP) || strIP.Length < 7 || strIP.Length > 15)
return false;
const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
var regex = new Regex(regFormat, RegexOptions.IgnoreCase);
return regex.IsMatch(strIP);
}

#endregion

#region 取客户端真实IP

public static string GetIPAddress
{
get
{
var result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(result))
{
if (result.IndexOf(".") == -1)
{
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["HTTP_X_REAL_IP"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
if (string.IsNullOrEmpty(result))
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}

#endregion


#region 获取公网IP

public static string GetNetIP()
{
string tempIP = "";
try
{
System.Net.WebRequest wr = System.Net.WebRequest.Create("http://ip.3322.org/");
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.GetEncoding("gb2312"));
tempIP = sr.ReadToEnd().ToString();//读取网站的数据
tempIP=tempIP.Split(‘\n‘)[0];
sr.Close();
s.Close();
}
catch
{
if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length > 1)
{
tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString();

}
if (string.IsNullOrEmpty(tempIP))
{
return GetIP();
}
}
return tempIP;
}

#endregion

#region 获取客户端IP地址

public static string GetIP()
{
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
}
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (string.IsNullOrEmpty(result))
{
return "0.0.0.0";
}
return result;
}

#endregion
}
}

获取客户端IP 和 外网IP

标签:ethos   space   就是   generic   gen   .net   for   end   数据   

原文地址:https://www.cnblogs.com/clj0102/p/9262404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!