獲取訪問者ip:
string userIP; // HttpRequest Request = HttpContext.Current.Request; HttpRequest Request = System.Web.HttpContext.Current.Request; // 如果使用代理,獲取真實IP if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "") userIP = Request.ServerVariables["REMOTE_ADDR"]; else userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (userIP == null || userIP == "") userIP = Request.UserHostAddress; return userIP;
獲取本機外網ip:
//獲取本機外網ip的url string getIpUrl = "http://www.ipip.net/ip.html";//網上獲取ip地址的網站 string tempip = ""; WebRequest wr = WebRequest.Create(getIpUrl); Stream s = wr.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.UTF8); string all = sr.ReadToEnd(); //讀取網站的數據 //解析出需要的數據 int start = all.IndexOf("<th colspan=\"3\">您的當前IP: <span style=\"color: rgb(243, 102, 102);\">"); int end = all.IndexOf("</span></th>"); tempip = all.Substring(start, end - start).Replace("<th colspan=\"3\">您的當前IP: <span style=\"color: rgb(243, 102, 102);\">", ""); sr.Close(); s.Close();
