IP地址详情
public static string GetClientIPKLocationKV()
{string ret = string.Empty;string LocationKV = string.Empty;List<string> KVs = new List<string>{"网站访问记录:"};try{HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://freegeoip.net/json/"+GetWebClientIp()));webReq.Method = "GET";HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);ret = sr.ReadToEnd();sr.Close();response.Close();}catch (Exception ex){return ex.Message;}var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(ret);foreach (var kv in dict){KVs.Add(kv.Key + ":" + kv.Value);}KVs.Add("访问时间:" + DateTime.Now.ToString());return string.Join("<br>", KVs.ToArray());
}
private static string GetWebClientIp(){string userIP = "";string CustomerIP = "";try{if (System.Web.HttpContext.Current == null|| System.Web.HttpContext.Current.Request == null|| System.Web.HttpContext.Current.Request.ServerVariables == null){return "";}CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];if (!string.IsNullOrEmpty(CustomerIP)){return CustomerIP;}CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];if (!String.IsNullOrEmpty(CustomerIP)){return CustomerIP;}if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null){CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];if (CustomerIP == null){CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];}}else{CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];}if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP)){return System.Web.HttpContext.Current.Request.UserHostAddress;}}catch { CustomerIP = "0.0.0.0"; }return CustomerIP == "::1" ? "0.0.0.0" : CustomerIP;}
Freegeoip api更新,要求用Token获取。更新方法。
public class Utills
{public static string GetIPLocationKV(){string ret = string.Emptystring LocationKV = string.EmptyList<string> KVs = new List<string>{"访问记录:"}try{HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://www.freegeoip.net/json"))webReq.Method = "GET"webReq.ContentType = "application/x-www-form-urlencoded"HttpWebResponse response = (HttpWebResponse)webReq.GetResponse()StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default)ret = sr.ReadToEnd()sr.Close()response.Close()}catch (Exception ex){return ex.Message}var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(ret)foreach (var kv in dict){KVs.Add(kv.Key + ":" + kv.Value)}KVs.Add("访问时间:" + DateTime.Now.ToString())return string.Join("<br>", KVs.ToArray())}public static string GetClientIPKLocationKV(){string ret = string.Emptystring LocationKV = string.EmptyList<string> KVs = new List<string>{"网站访问记录:"}try{//HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://freegeoip.net/json/"+GetWebClientIp()))HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(string.Format(ConfigurationManager.AppSettings["getIPstackURL"].ToString(), GetWebClientIp(), ConfigurationManager.AppSettings["ipstackkey"].ToString())))webReq.Method = "GET"HttpWebResponse response = (HttpWebResponse)webReq.GetResponse()StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default)ret = sr.ReadToEnd()sr.Close()response.Close()}catch (Exception ex){return ex.Message}var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(ret)foreach (var kv in dict){KVs.Add(kv.Key + ":" + kv.Value ?? "")}KVs.Add("访问时间:" + DateTime.Now.ToString())return string.Join("<br>", KVs.ToArray())}public string GetIPLocationCity(){string IP = ""string strHostName = ""strHostName = System.Net.Dns.GetHostName()IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName)IPAddress[] addr = ipEntry.AddressListIP = addr[2].ToString()//Initializing a new xml document object to begin reading the xml file returnedXmlDocument doc = new XmlDocument()doc.Load("http://www.freegeoip.net/json")XmlNodeList nodeLstCity = doc.GetElementsByTagName("City")IP = "" + nodeLstCity[0].InnerText + "<br>" + IPreturn IP}/// <summary>/// 获取web客户端ip/// </summary>/// <returns></returns>private static string GetWebClientIp(){string userIP = ""string CustomerIP = ""try{if (System.Web.HttpContext.Current == null|| System.Web.HttpContext.Current.Request == null|| System.Web.HttpContext.Current.Request.ServerVariables == null){return ""}//CDN加速后取到的IPCustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"]if (!string.IsNullOrEmpty(CustomerIP)){return CustomerIP}CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]if (!String.IsNullOrEmpty(CustomerIP)){return CustomerIP}if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null){CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]if (CustomerIP == null){CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]}}else{CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]}if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP)){return System.Web.HttpContext.Current.Request.UserHostAddress}}catch { CustomerIP = "0.0.0.0"return CustomerIP == "::1" ? "0.0.0.0" : CustomerIP}}