C# MaxMind/GeoIP调用位置类时出错
我正在成功地使用MaxMind GeoIP Legacy.NETAPI()Country类检索基于IP的国家代码。但是,我在尝试使用Location类时遇到了一些问题。下面是我的代码:C# MaxMind/GeoIP调用位置类时出错,c#,asp.net,.net,geoip,maxmind,C#,Asp.net,.net,Geoip,Maxmind,我正在成功地使用MaxMind GeoIP Legacy.NETAPI()Country类检索基于IP的国家代码。但是,我在尝试使用Location类时遇到了一些问题。下面是我的代码: try { // capture visitors IP string visitorIp = Model.ServerVariables["HTTP_X_Forwarded_For"]; visitorIp = String.IsNullOrWhiteSpace(visitorIp) ?
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>
}
试试看
{
//捕获访客IP
string visitorIp=Model.ServerVariables[“HTTP_X_Forwarded_For”];
visitorIp=String.IsNullOrWhiteSpace(visitorIp)?Request.UserHostAddress:visitorIp;
字符串geoIpPath=HttpContext.Current.Server.MapPath(“~/App\u Data/GeoIP.dat”);
LookupService ls=新的LookupService(geoIpPath,LookupService.GEOIP\u MEMORY\u CACHE);
//获取ip地址的国家/地区
国家c=ls.getCountry(visitori);
字符串countryCode=c.getCode()!=“--”?c.getCode():“US”;
//将国家代码捕获为cookie
if(Request.Cookies[“GeoIP”]==null)
{
HttpCookie cookie=新的HttpCookie(“GeoIP”,国家代码);
cookie.Expires=DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}
字符串geoicitypath=HttpContext.Current.Server.MapPath(“~/App_Data/GeoLiteCity.dat”);
LookupService lsCity=新的LookupService(geoIpCityPath,LookupService.GEOIP_内存_缓存);
位置l=ls.getLocation(visitorIp);
邮政编码=@l.postalCode.ToString()
}
捕获(例外e)
{
@e
}
跟踪字符串countryCode时不会出错。当我尝试使用Location类时,会收到以下错误消息:
System.ArgumentOutOfRangeException:需要非负数。
参数名称:System.Array.Copy处的长度(Array sourceArray,Int32
sourceIndex、数组destinationArray、Int32 destinationIndex、Int32
System.Array.Copy(Array sourceArray,
Int32 sourceIndex、数组destinationArray、Int32 destinationIndex、,
中LookupService.getLocation(Int64 ipnum)处的Int32长度)
~\Models\LookupService.cs:line
582中的LookupService.getLocation(IPAddress addr)
~\Models\LookupService.cs:line
443在ASP.\页面\视图\共享\地理IP\ cshtml.Execute()中
~\Views\Shared\u GeoIP.cshtml:第34行
想法?提前谢谢 我建议针对GeoIP遗留API提交一个bug。听起来读取数据库时出现了问题。您还可以尝试NuGet的版本,这是GitHub版本的一个分支 对于GeoIP2,您可以使用NuGet获得预编译的2.4.0包:
Install-Package MaxMind.GeoIP2 -Version 2.4.0
我建议针对GeoIP遗留API提交一个bug。听起来读取数据库时出现了问题。您还可以尝试NuGet的版本,这是GitHub版本的一个分支 对于GeoIP2,您可以使用NuGet获得预编译的2.4.0包:
Install-Package MaxMind.GeoIP2 -Version 2.4.0
您使用传统API而不是传统API有什么原因吗?前者总体上有点混乱。@oschwald我正在集成的项目仍然在.Net 4.0上,因此较新的版本现在可能不是一个选项(但愿不是这样)。如果您确实想使用GeoIP2 API,您可以使用2.4.0,这是支持.Net 4.0的最后一个版本。对于上述问题,你使用的是NuGet的版本吗?这是由第三方完成的,我认为,它已经过时,可能缺少一些国家,这可能导致上述问题。你在所有IP上都能看到吗?@oschwald我如何获得GeoIP2 API的v2.4.0?至于遗留API,我相信它是直接从Github获得的,而不是nuget。我将为各种IP运行更多的测试,但到目前为止,我所尝试的似乎都是错误的。谢谢你所有的输入顺便说一句!您使用传统API而不是传统API有什么原因吗?前者总体上有点混乱。@oschwald我正在集成的项目仍然在.Net 4.0上,因此较新的版本现在可能不是一个选项(但愿不是这样)。如果您确实想使用GeoIP2 API,您可以使用2.4.0,这是支持.Net 4.0的最后一个版本。对于上述问题,你使用的是NuGet的版本吗?这是由第三方完成的,我认为,它已经过时,可能缺少一些国家,这可能导致上述问题。你在所有IP上都能看到吗?@oschwald我如何获得GeoIP2 API的v2.4.0?至于遗留API,我相信它是直接从Github获得的,而不是nuget。我将为各种IP运行更多的测试,但到目前为止,我所尝试的似乎都是错误的。谢谢你所有的输入顺便说一句!我安装了Geoip2.4.0 NuGet软件包,它工作得非常好,谢谢!我将针对GeoIP遗留API提交一个bug。再次感谢您的帮助和帮助!我安装了Geoip2.4.0 NuGet软件包,它工作得非常好,谢谢!我将针对GeoIP遗留API提交一个bug。再次感谢您的帮助和帮助!