4AspNet IPInfo v1.0

IPInfo.GetInfoByHostAddress Method (Int64)

Retrieves country information by a raw format of IP address.

public CountryInfo GetInfoByHostAddress(
   long address
);

Parameters

address
The IP address in the raw format.

Return Value

The CountryInfo structure.

Remarks

Use this overloaded version of GetInfoByHostAddress method when you need to specify raw IP address as an argument.

If the IP address has a form aaa.bbb.ccc.ddd when raw IP address is a number obtained with a next formula:

raw_ip_address = aaa * 16777216L + bbb * 65536L + ccc * 256L + ddd.

When country information for the specified address is absent and ThrowIfNotFound property is set to false method will return CountryInfo structure without significant information. If ThrowIfNotFound is set to true, the exception of type CountryInfoNotFoundException will be thrown.

Exceptions

Exception TypeCondition
CountryInfoNotFoundExceptionThrown when property ThrowIfNotFound is set to true and there is no country information for a specified address.

Example

The following example demonstrates using of GetInfoByHostAddress specifying the raw IP address as an argument.

public string GetCountryName(byte[] addressBytes)
{
    if(addressBytes.Length != 4)
        throw new ArgumentException();

    long address = addressBytes[0] * 16777216L + addressBytes[1] * 65536L +
    addressBytes[2] * 256L + addressBytes[3];

    // assume what license is already initialized

    IPInfo ipi = new IPInfo();
    CountryInfo ci = ipi.GetInfoByHostAddress(address);

    return ci.CountryName;
}

See Also

IPInfo Class | ForAspNet.IPInfo Namespace | IPInfo.GetInfoByHostAddress Overload List