我将如何确定一个特定IP地址源于使用C#的国家。我需要使用它来检查连接是否源自特定的国家。如何确定一个IP地址是否属于一个国家
回答
您可以在您的项目中使用此SQL数据来确定:IP address geolocation SQL database。下载这些数据并将其导入到数据库中以在本地运行检查。
或者您可以使用它们的免费API返回包含国家代码和国家/地区名称的XML。你会做出与IP地址,你要检查,如在这个例子中,下列URL的请求:
http://ipinfodb.com/ip_query_country.php?ip=74.125.45.100
返回:
<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
</Response>
进行批量查找请注意IP分配偶尔会更改。所以记得偶尔更新你的数据库。 (你需要的更新频率取决于你在做什么;对于简单的统计数据,你可以决定年度更新是否可以)。 – user9876 2010-02-10 14:34:11
现在已经发生了变化:您必须立即注册(免费)才能使用他们的API。看到这里:http://ipinfodb.com/ip_location_api.php – 2011-04-27 11:52:44
如果你不想使用像hostip.info这样的API,那么我建议订阅maxmind并在本地运行一个主机查找数据库。
这里是一个免费的IP Address to Country database。
ip2cc - 通过IP地址查找国家和俄罗斯地区Python模块使用脚本从最新的官方数据创建数据库。
这Python
实用负载(如经常只要你喜欢)了最新从Regional Internet Registry网站(arin
,ripencc
,apnic
,lacnic
,afrinic
)的信息,shown in the source:
url_template = 'ftp://ftp.ripe.net/pub/stats/%s/delegated-%s-latest'
sources = {}
for name in ('arin', 'ripencc', 'apnic', 'lacnic', 'afrinic'):
sources[name] = url_template % (name, name)
一旦数据加载,查询可以回答离线并且很快。 可以很容易地修改,直接回答原来的问题,或从命令行使用返回国家IP address
属于。
你可以使用另一种服务是我自己的,http://ipinfo.io,它返回的位置,组织和其他信息:
$ curl ipinfo.io/8.8.8.8
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"loc": "37.385999999999996,-122.0838",
"org": "AS15169 Google Inc.",
"city": "Mountain View",
"region": "California",
"country": "US",
"phone": 650
}
更多信息请参见http://ipinfo.io/developers。
手动获取json:“http://ipinfo.io/8.8.8.8/json” – 2016-04-25 08:10:32
只需拨打一个简单的API即可。https://ipapi.co/8.8.8.8/country/
美国
这里有一个working fiddle C#示例:
using System;
using System.Net;
using System.IO;
using System.Text;
public class Program
{
public static void Main()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://ipapi.co/8.8.8.8/country/");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var reader = new System.IO.StreamReader(response.GetResponseStream(), ASCIIEncoding.ASCII);
Console.WriteLine(reader.ReadToEnd());
}
}
以下是如何与https://ipdata.co
//Common testing requirement. If you are consuming an API in a sandbox/test region, uncomment this line of code ONLY for non production uses.
//System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//Be sure to run "Install-Package Microsoft.Net.Http" from your nuget command line.
using System;
using System.Net.Http;
var baseAddress = new Uri("https://api.ipdata.co/78.8.53.5");
using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
using(var response = await httpClient.GetAsync("undefined"))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
通过卷曲
做到这一点curl https://api.ipdata.co/78.8.53.5
{
"ip": "78.8.53.5",
"city": "G\u0142og\u00f3w",
"region": "Lower Silesia",
"region_code": "DS",
"country_name": "Poland",
"country_code": "PL",
"continent_name": "Europe",
"continent_code": "EU",
"latitude": 51.6461,
"longitude": 16.1678,
"asn": "AS12741",
"organisation": "Netia SA",
"postal": "67-200",
"currency": "PLN",
"currency_symbol": "z\u0142",
"calling_code": "48",
"flag": "https://ipdata.co/flags/pl.png",
"emoji_flag": "\ud83c\uddf5\ud83c\uddf1",
"time_zone": "Europe/Warsaw",
"is_eu": true,
"suspicious_factors": {
"is_tor": false
}
}⏎
对于离线数据库,你可以得到免费的IP2Location LITE DB1
要创建表
CREATE DATABASE ip2location
GO
USE ip2location
GO
CREATE TABLE [ip2location].[dbo].[ip2location_db1](
[ip_from] float NOT NULL,
[ip_to] float NOT NULL,
[country_code] nvarchar(2) NOT NULL,
[country_name] nvarchar(64) NOT NULL,
) ON [PRIMARY]
GO
CREATE INDEX [ip_from] ON [ip2location].[dbo].[ip2location_db1]([ip_from]) ON [PRIMARY]
GO
CREATE INDEX [ip_to] ON [ip2location].[dbo].[ip2location_db1]([ip_to]) ON [PRIMARY]
GO
要导入数据
BULK INSERT [ip2location].[dbo].[ip2location_db1]
FROM 'C:\[path to your CSV file]\IP2LOCATION-LITE-DB1.CSV'
WITH
(
FORMATFILE = 'C:\[path to your DB1.FMT file]\DB1.FMT'
)
GO
对于FMT文件
10.0
5
1 SQLCHAR 0 1 "\"" 0 first_double_quote Latin1_General_CI_AI
2 SQLCHAR 0 20 "\",\"" 1 ip_from ""
3 SQLCHAR 0 20 "\",\"" 2 ip_to ""
4 SQLCHAR 0 2 "\",\"" 3 country_code Latin1_General_CI_AI
5 SQLCHAR 0 64 "\"\r\n" 4 country_name Latin1_General_CI_AI
FMT代码的第一行指示bcp的版本。请根据您安装的MS-SQL更改版本。
的SQL Server 2016 12.0
的SQL Server 2014 12.0
SQL Server 2012的11.0
的SQL Server 2008/2008 R2 10.0
SQL Server 2005的9.0
的SQL Server 2000 8.0
SQL Server 7.0 7.0
SQL Server 6.5中6.5
C#代码查询MSSQL
using System.Data.SqlClient;
using System.Numerics;
using System.Net;
using System.Text;
public class Form1 {
private void Form1_Load(object sender, System.EventArgs e) {
string ip = "8.8.8.8";
this.IP2Location(ip);
}
private void IP2Location(string myip) {
IPAddress address = null;
if (IPAddress.TryParse(myip, address)) {
byte[] addrBytes = address.GetAddressBytes();
this.LittleEndian(addrBytes);
UInt32 ipno = 0;
ipno = BitConverter.ToUInt32(addrBytes, 0);
string sql = "SELECT TOP 1 * FROM ip2location_db1 WHERE ip_to >= \'" + ipno.ToString() + "\'";
object conn = new SqlConnection("Server=yourserver;Database=yourdatabase;User Id=youruserid;Password=yourpassword;");
object comm = new SqlCommand(sql, conn);
SqlDataReader reader;
comm.Connection.Open();
reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
int x = 0;
object sb = new StringBuilder(250);
if (reader.HasRows) {
if (reader.Read()) {
for (x = 0; (x <= (reader.FieldCount() - 1)); x++) {
sb.Append((reader.GetName(x) + (": " + (reader.GetValue(x) + "\r\n"))));
}
}
}
reader.Close();
MsgBox(sb.ToString());
}
}
private void LittleEndian(ref byte[] byteArr) {
if (BitConverter.IsLittleEndian) {
List<byte> byteList = new List<byte>(byteArr);
byteList.Reverse();
byteArr = byteList.ToArray();
}
}
}
- 1. 如何确定IP地址属于哪个公司?
- 2. 如何确定多个虚拟地址是否属于同一页面?
- 3. 确定IP地址是否为蜂窝IP地址
- 4. 每个国家/地区计算不同的IP地址
- 5. 如何根据国家IP地址重定向域名
- 6. 确定一个地址是否属于堆或堆栈或寄存器
- 7. 如何用页面中的IP地址替换IP地址中的IP地址(例如“国家,地区,城市”)
- 8. C# - 确定某个IP地址范围包含一个特定的地址
- 9. 与国家关联IP地址
- 10. SQL IP地址到国家代码
- 11. 按IP地址查找国家
- 12. 如何快速确定点(纬度和经度)是否在一个国家内?
- 13. 如何从java中给定ip获得下一个ip地址?
- 14. 将IP数据包从一个IP地址转发到另一个IP地址
- 15. 确定两个IP地址是否可以通信
- 16. Ping一个IP地址
- 17. 如何限制谷歌地方只限于一个国家
- 18. 子网IP地址属于哪个?
- 19. 如何根据IP地址确定国籍?
- 20. Python包:获取基于地址的国家(非ip)
- 21. 脚本发现IP地址是否属于IP范围
- 22. 如何将IP地址转换为国家?
- 23. 如何知道设备IP地址的国家名?
- 24. 如何知道Google Analytics(分析)中从IP地址/国家访问哪个URL
- 25. 确定是否一个属性是一个自动财产
- 26. 如何做一个基于IP地址的Apache2重写
- 27. 地址按国家/地区
- 28. 一个主机的多个IP地址
- 29. 是否始终为同一个国家/地区使用ip范围?例如:111.111.111.x =总是<country>
- 30. IP地理位置确实显示了物理位置还是实际上可以从一个国家“移动”到另一个国家?
我需要的数据可脱机使用,因为它会被用于统计。因此,我将使用sql选项 – RC1140 2009-08-13 05:10:36
您也可以使用https://ipdata.co – Jonathan 2018-02-18 20:23:26