避免使用 InetAddress - 以网络字节顺序获取原始 IP 地址
我正在尝试在 Google App Engine 上使用 MaxMind GeoLite Country 数据库。但是,我很难让 Java API 正常工作,因为它依赖于 InetAddress 类,而该类无法在 App Engine 上使用。
但是,我不确定是否有一个简单的解决方法,因为它似乎只使用 InetAddress 类来确定给定主机名的 IP。就我而言,主机名始终是 IP。
我需要一种方法,将表示为字符串的 IP 地址转换为网络字节顺序的字节数组(InetAddress 类的 addr.getAddress() 方法提供)。
这是当前 API 使用的代码,我需要找到一种方法来删除对 InetAddress 的所有引用,同时确保它仍然有效!
感谢您抽出时间。
/**
* Returns the country the IP address is in.
*
* @param ipAddress String version of an IP address, i.e. "127.0.0.1"
* @return the country the IP address is from.
*/
public Country getCountry(String ipAddress) {
InetAddress addr;
try {
addr = InetAddress.getByName(ipAddress);
} catch (UnknownHostException e) {
return UNKNOWN_COUNTRY;
}
return getCountry(bytesToLong(addr.getAddress()));
}
I am trying to use the MaxMind GeoLite Country database on the Google App Engine. However, I am having difficulty getting the Java API to work as it relies on the InetAddress class which is not available to use on the App Engine.
However, I am not sure if there is a simple workaround as it appears it only uses the InetAddress class to determine the IP of a given hostname. In my case, the hostname is always an IP anyway.
What I need is a way to convert an IP address represented as a String into a byte array of network byte order (which the addr.getAddress() method of the InetAddress class provides).
This is the code the current API uses, I need to find a way of removing all references to InetAddress whilst ensuring it still works!
Thanks for your time.
/**
* Returns the country the IP address is in.
*
* @param ipAddress String version of an IP address, i.e. "127.0.0.1"
* @return the country the IP address is from.
*/
public Country getCountry(String ipAddress) {
InetAddress addr;
try {
addr = InetAddress.getByName(ipAddress);
} catch (UnknownHostException e) {
return UNKNOWN_COUNTRY;
}
return getCountry(bytesToLong(addr.getAddress()));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GeoIPCountryWhois 数据库中的所有 12000 多个条目均采用以下形式:
将您拥有的点分四边形地址分解为子字符串,并以这种方式对它们进行范围检查。
All 12000+ entries in the GeoIPCountryWhois database are of the form:
break the dotted quad address you have into substrings and range check them that way.