新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
利用C#怎么對客戶端的IPv4地址進行獲取-創(chuàng)新互聯(lián)
本篇文章為大家展示了利用C#怎么對客戶端的IPv4地址進行獲取,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
網(wǎng)上找了一些獲取客戶端IP的方法,但本地測試時,返回的是IPv6的表示方法"::1":
Host文件里面:# ::1 localhost
后來找了獲取IPv4的方法就可以了,比較好用:
public static string GetClientIPv4Address() { string ipv4 = String.Empty; foreach (IPAddress ip in Dns.GetHostAddresses(GetClientIP())) { if (ip.AddressFamily.ToString() == "InterNetwork") { ipv4 = ip.ToString(); break; } } if (ipv4 != String.Empty) { return ipv4; } // 利用 Dns.GetHostEntry 方法,由獲取的 IPv6 位址反查 DNS 紀錄, // 再逐一判斷何者為 IPv4 協(xié)議,即可轉(zhuǎn)為 IPv4 位址。 foreach (IPAddress ip in Dns.GetHostEntry(GetClientIP()).AddressList) //foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName())) { if (ip.AddressFamily.ToString() == "InterNetwork") { ipv4 = ip.ToString(); break; } } return ipv4; } public static string GetClientIP() { if (null == HttpContext.Current.Request.ServerVariables["HTTP_VIA"]) { return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } else { return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } }
上述內(nèi)容就是利用C#怎么對客戶端的IPv4地址進行獲取,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標題名稱:利用C#怎么對客戶端的IPv4地址進行獲取-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://biofuelwatch.net/article/cspppp.html