MaxMind/GeoIP 调用位置时出错 class
MaxMind/GeoIP Error calling Location class
我成功使用 MaxMind GeoIP Legacy .NET API (https://github.com/maxmind/geoip-api-csharp2) 国家 Class 检索基于 IP 的国家代码。但是,我 运行 遇到了尝试使用位置 class 的问题。下面是我的代码:
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 跟踪无误。当我尝试使用位置 class 时,我收到以下错误消息:
System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: length at System.Array.Copy(Array sourceArray, Int32
sourceIndex, Array destinationArray, Int32 destinationIndex, Int32
length, Boolean reliable) at System.Array.Copy(Array sourceArray,
Int32 sourceIndex, Array destinationArray, Int32 destinationIndex,
Int32 length) at LookupService.getLocation(Int64 ipnum) in
~\Models\LookupService.cs:line
582 at LookupService.getLocation(IPAddress addr) in
~\Models\LookupService.cs:line
443 at ASP._Page_Views_Shared__GeoIP_cshtml.Execute() in
~\Views\Shared_GeoIP.cshtml:line 34
想法?提前致谢!
我建议针对 GeoIP Legacy API 提交错误。听起来好像读取数据库有问题。您也可以尝试 NuGet 的版本,它是 GitHub.
上版本的一个分支
至于 GeoIP2,您可以使用 NuGet 获得预编译的 2.4.0 包:
Install-Package MaxMind.GeoIP2 -Version 2.4.0
我成功使用 MaxMind GeoIP Legacy .NET API (https://github.com/maxmind/geoip-api-csharp2) 国家 Class 检索基于 IP 的国家代码。但是,我 运行 遇到了尝试使用位置 class 的问题。下面是我的代码:
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 跟踪无误。当我尝试使用位置 class 时,我收到以下错误消息:
System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length) at LookupService.getLocation(Int64 ipnum) in ~\Models\LookupService.cs:line 582 at LookupService.getLocation(IPAddress addr) in ~\Models\LookupService.cs:line 443 at ASP._Page_Views_Shared__GeoIP_cshtml.Execute() in ~\Views\Shared_GeoIP.cshtml:line 34
想法?提前致谢!
我建议针对 GeoIP Legacy API 提交错误。听起来好像读取数据库有问题。您也可以尝试 NuGet 的版本,它是 GitHub.
上版本的一个分支至于 GeoIP2,您可以使用 NuGet 获得预编译的 2.4.0 包:
Install-Package MaxMind.GeoIP2 -Version 2.4.0