public static IpData GetIpData(string url) { string html = string.Empty; string urlipstack = "http://api.ipstack.com/" + url + "?access_key=db291975c9692aa63b7b10656ec145c3"; Console.WriteLine(urlipstack); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlipstack); request.AutomaticDecompression = DecompressionMethods.GZip; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { html = reader.ReadToEnd(); } Console.WriteLine(html); var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; IpStackData ipStackData = JsonConvert.DeserializeObject <IpStackData>(html, settings); IpData result = new IpData { Url = url, Ip = ipStackData.ip, City = ipStackData.city, Continent_code = ipStackData.continent_code, Continent_name = ipStackData.continent_name, Country_code = ipStackData.country_code, Country_name = ipStackData.country_name, Ip_type = ipStackData.type, Latitude = ipStackData.latitude, Longitude = ipStackData.longitude, Region_code = ipStackData.region_code, Region_name = ipStackData.region_name, Zip = ipStackData.zip }; return(result); }
private static bool IsNewNAS(string url) { IpData ipdata = GetIpData(FindBaseUrl(url)); conn.Open(); bool isNewNas = false; string query = "SELECT COUNT(*) FROM nas WHERE ip='" + ipdata.Ip + "'"; var cmd = new MySqlCommand(query, conn); if (Convert.ToInt32(cmd.ExecuteScalar()) == 0) { currentNASid = GenerateGUID(); query = "INSERT INTO `nascan`.`nas` (`id`, `url`, `ip`, `ip_type`, `continent_code`, `continent_name`, `country_code`, `country_name`, `region_code`, `region_name`, `city`, `zip`, `latitude`, `longitude`) VALUES ('" + currentNASid + "', '" + ipdata.Url + "', '" + ipdata.Ip + "', '" + ipdata.Ip_type + "', '" + ipdata.Continent_code + "', '" + ipdata.Continent_name + "', '" + ipdata.Country_code + "', '" + ipdata.Country_name + "', '" + ipdata.Region_code + "', '" + ipdata.Region_name + "', '" + ipdata.City + "', '" + ipdata.Zip + "', '" + ipdata.Latitude + "', '" + ipdata.Longitude + "')"; cmd = new MySqlCommand(query, conn); cmd.ExecuteNonQuery(); isNewNas = true; } conn.Close(); return(isNewNas); }