2010-12-04 55 views
1

我需要使用经典asp 3.0获取访问者国家信息,我使用下面的代码,但它返回了XX而不是国家名称。对此有任何建议/帮助。使用经典ASP 3.0获取访问者国家信息

<% 
     URL = "http://api.hostip.info/country.php?ip=" & Request.ServerVariables("REMOTE_ADDR") 
     Set conn = Server.CreateObject("MSXML2.ServerXMLHTTP") 
     conn.open "GET", URL, False, "", "" 
     conn.send 
     UserCountry = conn.ResponseText   
     Response.Write(UserCountry) 

%> 

我还设置了一个包含上面代码的视图页面。

http://www.datingmaze.co.uk/rac.asp

如果我尝试http://api.hostip.info/country.php?ip=12.215.42.19它给了我正确的输出,但如果我试图http://api.hostip.info/country.php?ip=119.152.136.247让我即XX尽管我提供了正确的IP地址错误的输出。

在此先感谢。

回答

2

您可以为此使用GeoIP。

他们有一个免费的COM API,你可以使用:

<% 
if Request.Form("values") = "Test Values" then 
hostname = "www.yahoo.com" 
else 
    hostname = Request.Form("hostname") 
end if 

if Request.Form("submit") = "Submit" then 

    set geoip = Server.CreateObject("GeoIPCOMEx.GeoIPEx") 

    geoip.set_db_path("C:\Program Files\GeoIP\") 
geoip.find_by_name(hostname) 
city = geoip.city 

    Response.Write("<table cellpadding=2 border=1><tr><th colspan=2>Results</th></tr>") 
    Response.Write("<tr><td>Hostname</td><td>" + hostname + "</td></tr>") 
    Response.Write("<tr><td>GeoIP City Value</td><td>" + city + "</td></tr>") 
    Response.Write("</table>") 
end if 
%> 

http://www.maxmind.com/app/com

http://www.maxmind.com/GeoIP-COM-1.3.zip

他们的COM API公开以下:

Methods: 
bool set_db_path(string path) (must be set before any other operations, true if all dbs found) 
bool find_by_addr(string ipvalue) (return true if address found, sets all properties) 
bool find_by_name(string dns_name) (-"-) 

的数据,你在查找后会收到:

Properties: 
country_code (2 chars; "LN" if non-routed addr, "LH" if localhost) 
country_code3 (3 chars) 
country_name ("Local Area Network" if non-routed addr,"Localhost" if localhost) 
region (2 chars, state abbrev for US/Canada, FIPS 10-4 region code for others) 
city 
postal_code (max 6 chars, US and Canada only) 
latitude (real number) 
longitude (real number) 
dma_code (integer) 
area_code (integer) 

因此,不是使用find_by_name,而是使用find_by_addr。这将根据IPv4地址查找国家。

这是一个更好的解决方案,因为依赖于远程第三方提供商可能有风险。他们的网站可能会往下走,是在重负载下,等

你可以下载他们的IP /国家数据库的免费版本在这里:

http://www.maxmind.com/app/geolitecountry