Я успешно использую API-интерфейс MaxMind GeoIP Legacy .NET (https://github.com/maxmind/geoip-api-csharp2) Country Class для извлечения кода страны на основе IP. Однако я сталкиваюсь с проблемами, использующими класс Location. Ниже приводится мой код:MaxMind/GeoIP Ошибка вызова Класс местоположения
try
{
// capture visitors IP
string visitorIp = Model.ServerVariables["HTTP_X_Forwarded_For"];
visitorIp = String.IsNullOrWhiteSpace(visitorIp) ? Request.UserHostAddress : visitorIp;
string geoIpPath = HttpContext.Current.Server.MapPath("~/App_Data/GeoIP.dat");
LookupService ls = new LookupService(geoIpPath, LookupService.GEOIP_MEMORY_CACHE);
//get country of the ip address
Country c = ls.getCountry(visitorIp);
string countryCode = c.getCode() != "--" ? c.getCode() : "US";
// Capture country code as cookie
if (Request.Cookies["GeoIP"] == null)
{
HttpCookie cookie = new HttpCookie("GeoIP", countryCode);
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}
string geoIpCityPath = HttpContext.Current.Server.MapPath("~/App_Data/GeoLiteCity.dat");
LookupService lsCity = new LookupService(geoIpCityPath, LookupService.GEOIP_MEMORY_CACHE);
Location l = ls.getLocation(visitorIp);
<h1>Postal Code = @l.postalCode.ToString()</h1>
}
catch (Exception e)
{
<p>@e</p>
}
Строка countryCode отслеживается без ошибок. Когда я пытаюсь использовать класс Location, я получаю следующее сообщение об ошибке:
System.ArgumentOutOfRangeException: Требуется неотрицательное число. Имя параметра: длина в System.Array.Copy (Array sourceArray, Int32 sourceIndex, массив destinationArray, Int32, Int32 destinationIndex длина, надежного булева) при System.Array.Copy (Array sourceArray, Int32 sourceIndex, массив destinationArray, Int32 destinationIndex, длина Int32) в LookupService.getLocation (Int64 ipnum) в ~ \ Models \ LookupService.cs: линия 582 на LookupService.getLocation (IPAddress адр) в ~ \ Models \ LookupService.cs: линия 443 на ASP ._Page_Views_Shared__GeoIP_cshtml.Execute() в ~ \ Views \ Shared_GeoIP.cshtml: строка 34
Мысли? Заранее спасибо!
Есть причина, почему вы используете устаревший API, а не [GeoIP2 API] (HTTPS://github.com/maxmind/GeoIP2-dotnet)? Первый - это вообще беспорядок. –
@oschwald Проект, с которым я интегрируюсь, по-прежнему включен .Net 4.0, поэтому новая версия может быть не вариантом (сейчас это не так). – OtoNoOto
Если вы действительно хотите использовать API GeoIP2, вы можете использовать версию 2.4.0, которая является последней версией для поддержки .NET 4.0. –