您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
Solution 1:
#region 通过IP得到IP所在地省市 ///<summary> /// 通过IP得到IP所在地省市(Porschev) ///</summary> ///<param name="ip">Ip地址</param> ///<returns></returns> protected string GetAdrByIp(string ip) { try { string url = "http://www.ip138.com/ips1388.asp?ip=" + ip + "&action=2"; string regStr = "(?<=<ul class=\\\"ul1\\\"><li>.*:).*(?=\\s.*</li><li>)"; 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源码信息 ///<summary> /// 获取HTML源码信息(Porschev) ///</summary> ///<param name="url">获取地址</param> ///<returns>HTML源码</returns> 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; } #endregion
Tips 1:
/// <summary> /// 获取用户ip地址,避免ip代理 /// </summary> /// <returns>用户ip地址</returns> 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; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。