IP Geolocation API 활용

Jinho Lee·2024년 9월 11일
0

IP Geolocation API

  • IP 주소를 바탕으로 위치 등의 정보를 가져오는 API

무료로 사용가능한 IP

Json으로 데이터 받기

IP Geolocation 활용 예시 코드 - C#, JSON

		/// <summary>
        /// 해당 아이피의 정보를 가져옴
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static string GetGeolocation(string ip)
        {
            string result = "";
            string input = new WebClient().DownloadString("http://ip-api.com/json/" + ip);

            try
            {
                JObject JObj = ConvertManager.ToJObject(input);
                result += "country: " + ConvertManager.ToString(JObj, "country") + "\n";
                result += "regionName: " + ConvertManager.ToString(JObj, "regionName") + "\n";
                result += "city: " + ConvertManager.ToString(JObj, "city") + "\n";
                result += "zip: " + ConvertManager.ToString(JObj, "zip") + "\n";
                result += "lat: " + ConvertManager.ToString(JObj, "lat") + "\n";
                result += "lon: " + ConvertManager.ToString(JObj, "lon") + "\n";
                result += "timezone: " + ConvertManager.ToString(JObj, "timezone") + "\n";
                result += "isp: " + ConvertManager.ToString(JObj, "isp") + "\n";
                result += "org: " + ConvertManager.ToString(JObj, "org") + "\n";
                result += "as: " + ConvertManager.ToString(JObj, "as") + "\n";
                Debug.Instance.WriteLine(result);
            }
            catch (Exception e)
            {
                Console.WriteLine("GetGeolocation() -> " + "input: " + input);
            }
            return result;
        }

0개의 댓글