我们用 Request.ServerVariables( "REMOTE_ADDR ") 来取得客户端的IP地址。但是,如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables( "HTTP_X_FORWARDED_FOR ") 来读取。
要注意的是:并不是每个代理服务器都能用 Request.ServerVariables( "HTTP_X_FORWARDED_FOR ") 来读取客户端的真实IP,有些用此方法读取到的仍然是代理服务器的IP。如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ( "HTTP_X_FORWARDED_FOR ") 取到的值将是空的。
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.IO;
- using System.Net;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Text;
- using System.Text.RegularExpressions;
- /// <summary>
- /// 简单获取IP地址
- /// </summary>
- /// <returns></returns>
- public string getIp()
- {
- string guestIp = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
- if (string.IsNullOrEmpty(guestIp))
- {
- //没有代理IP则直接读取连接客户端IP
- guestIp = Request.ServerVariables["REMOTE_ADDR"];
- }
- if (string.IsNullOrEmpty(guestIp))
- {
- guestIp = Request.UserHostAddress;
- }
- return guestIp;
- }
- 有看到一种比较全面的写法
- /// <summary>
- /// 获取IP地址
- /// </summary>
- /// <returns></returns>
- public static string getIpAddr()
- {
- string result = String.Empty;
- result = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
- // 如果使用代理,获取真实IP
- if (result != null && result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式
- result = null;
- else if (result != null)
- {
- if (result.IndexOf(",") != -1)
- {
- //有“,”,估计多个代理,则取第一个不是内网的IP
- result = result.Replace(" ", "").Replace("'", "");
- string[] tmpIp = result.Split(",;".ToCharArray());
- for (int i = 0; i < tmpIp.Length; i++)
- {
- if (IsIpAddress(tmpIp[i]) && tmpIp[i].Substring(0, 3) != "10." && tmpIp[i].Substring(0, 8) != "192.168." && tmpIp[i].Substring(0, 7) != "172.31.")
- {
- return tmpIp[i]; //找到不是内网的地址
- }
- }
- }
- else if (IsIpAddress(result)) //代理即是IP格式
- return result;
- else
- result = null; //代理中的内容 非IP,取IP
- }
- if (string.IsNullOrEmpty(result))
- result = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- if (string.IsNullOrEmpty(result))
- result = System.Web.HttpContext.Current.Request.UserHostAddress;
- return result;
- }
- /// <summary>
- /// 正则判断是否是IP地址格式 0.0.0.0
- /// </summary>
- /// <param name="strIp">待判断的IP地址</param>
- /// <returns>true or false</returns>
- private static bool IsIpAddress(string strIp)
- {
- if (strIp == null || strIp == string.Empty || strIp.Length < 7 || strIp.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(strIp);
- }
- 第三方获取客户端公网IP
- /// <summary>
- /// 获取客户端公网IP
- /// </summary>
- /// <returns></returns>
- public string getPubIp()
- {
- string strUrl = "http://iframe.ip138.com/ic.asp";
- Uri uri = new Uri(strUrl);
- WebRequest wr = WebRequest.Create(uri);
- Stream s = wr.GetResponse().GetResponseStream();
- StreamReader sr = new StreamReader(s, Encoding.Default);
- string all = sr.ReadToEnd();
- int i = all.IndexOf("[") + 1;
- string tempip = all.Substring(i, 15);
- string ip = tempip.Replace("]", "").Replace(" ", "");
- return ip;
- }