您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。