C#獲取訪問者ip和獲取本機ip地址


獲取訪問者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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM