asp.net根据ip获得城市地址
asp.net根据ip获得城市地址
Solution 1:
创新互联公司是一家专注于做网站、成都做网站与策划设计,巴林左旗网站建设哪家好?创新互联公司做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:巴林左旗等地区。巴林左旗做网站价格咨询:13518219792
#region 通过IP得到IP所在地省市 ////// 通过IP得到IP所在地省市(Porschev) /// ///Ip地址 ///protected string GetAdrByIp(string ip) { try { string url = "http://www.ip138.com/ips1388.asp?ip=" + ip + "&action=2"; string regStr = "(?<=
- .*:).*(?=\\s.*
- )"; string html = GetHtml(url);
//得到网页源码
Regex reg = new Regex(regStr, RegexOptions.None);
Match ma = reg.Match(html);
return ma.Value.Substring(ma.Value.IndexOf("省") + 1).Trim();
}
catch
{
return "";
}
}
#endregion #region 获取HTML源码信息
///
/// 获取HTML源码信息(Porschev) /// ///获取地址 ///HTML源码 protected string GetHtml(string url) { string _value = ""; HttpWebRequest req; HttpWebResponse res; Stream ReceiveStream; StreamReader sr; req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; req.KeepAlive = false; req.Referer = url; req.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; res = req.GetResponse() as HttpWebResponse; ReceiveStream = res.GetResponseStream(); sr = new StreamReader(ReceiveStream, Encoding.GetEncoding("GB2312")); _value = HttpUtility.UrlDecode(sr.ReadToEnd()); sr.Close(); ReceiveStream.Close(); res.Close(); return _value; } #endregionTips 1:
///
/// 获取用户ip地址,避免ip代理 /// ///用户ip地址 public string GetIp() { string result = String.Empty; result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } return result; }
新闻名称:asp.net根据ip获得城市地址
文章出自:http://hbruida.cn/article/ijdogg.html