获取客户端IP 和 外网IP
Posted clj0102
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取客户端IP 和 外网IP相关的知识,希望对你有一定的参考价值。
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(‘
‘)[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的主要内容,如果未能解决你的问题,请参考以下文章