C# | Get My REAL External Public IP Address (not 127 or 192 stuff)
Yes, there is a way.
No 127.0.0.1 or 192.168.0.1 stuff.
Contact whatsmyip.com and they'll tell you!
Thanks to this guys at dreamincode.net
http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/
Update:
The function appears to return the server IP address.
How to get both CLIENT and SERVER public IP Addresses?
Update: Added IP v6 addresses condition
No 127.0.0.1 or 192.168.0.1 stuff.
Contact whatsmyip.com and they'll tell you!
public static IPAddress GetExternalIp() { string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } IPAddress externalIp = IPAddress.Parse(requestHtml); return externalIp; }
Thanks to this guys at dreamincode.net
http://www.dreamincode.net/forums/topic/24692-showing-the-external-ip-address-in-c%23/
Update:
The function appears to return the server IP address.
How to get both CLIENT and SERVER public IP Addresses?
string serverIP = GetExternalIp().ToString(); string clientIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if ((clientIP == null) || (clientIP == "")) { clientIP = Request.ServerVariables["REMOTE_ADDR"]; } //private IPs: 10.X, 172.X, 192.168.X if ((clientIP.Contains("127.0.0.1")) || (clientIP.StartsWith("192.168")) || (clientIP.StartsWith("10.")) || (clientIP.StartsWith("172.") || (clientIP.Contains(":")) )) { //I'm internal to the server clientIP = serverIP; }
Update: Added IP v6 addresses condition
Thanks!!
ReplyDeleteGood Post...Its very informative, Thanks for sharing the blog...Keep updating the blog. see this blog for top Programing Languages for Development of the yaear
ReplyDelete