using System.Net.Http.Headers;
public static string GetIPDecimalValueIfV6(string clientIP, out bool isIpV6)
if (IPAddress.TryParse(clientIP, out IPAddress address))
isIpV6 = address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6;
byte[] bytes = address.GetAddressBytes();
StringBuilder bldr = new StringBuilder();
for (int i = 0; i < 16; i += 2)
bldr.AppendFormat("{0:x2}{1:x2}:", bytes[i], bytes[i + 1]);
return bldr.ToString().Replace(":", string.Empty);
public static BigInteger IPToNumber(string IpAddress, bool isIpV6)
if (string.IsNullOrEmpty(IpAddress) || IpAddress == "::1")
BigInteger ipIntValue = BigInteger.Parse(IpAddress, System.Globalization.NumberStyles.HexNumber);
string[] arrDec = IpAddress.Split('.');
for (int i = arrDec.Length - 1; i >= 0; i--)
num += ((int.Parse(arrDec[i]) % 256) * Math.Pow(256, (3 - i)));
return new BigInteger(num);
public static void Main()
string ipcliente = "2001:200:ffff:ffff:ffff:ffff:ffff:ffff";
string _ipcliente = "2001:200:ffff:ffff:ffff:ffff:ffff:ffff";
Console.WriteLine("IP: {0}", ipcliente);
string tableToSearch = "paisip";
ipcliente = GetIPDecimalValueIfV6(ipcliente, out bool isIpV6);
Console.WriteLine("IP: {0}", ipcliente);
tableToSearch = "paisipv6";
if (ipcliente != string.Empty && ipcliente != null)
IP = IPToNumber(ipcliente, isIpV6);
Console.WriteLine("IP DECIMAL: {0}", IP);
Console.WriteLine("EJEMPLO LLAMADO A http://ip-api.com/json/{0}", _ipcliente);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://ip-api.com/json/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(_ipcliente).GetAwaiter().GetResult();
Console.WriteLine("RESULT {0} ", response.StatusCode);
if (response.IsSuccessStatusCode)
Console.WriteLine("RESPONSE {0}", response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
Console.WriteLine("EJEMPLO LOCALIZACION RANGO DE IP ");
IPNetwork ipnetwork = IPNetwork.Parse("2a00:4920::/29");
Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast);
Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable);
Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable);
Console.WriteLine("Usable : {0}", ipnetwork.Usable);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);
client = new HttpClient();
client.BaseAddress = new Uri("http://ip-api.com/json/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage _response = client.GetAsync(ipnetwork.FirstUsable.ToString()).GetAwaiter().GetResult();
Console.WriteLine("RESULT {0} ", _response.StatusCode);
_response = client.GetAsync(ipnetwork.LastUsable.ToString()).GetAwaiter().GetResult();
Console.WriteLine("RESULT {0} ", _response.StatusCode);