Get user location by IP address

前端 未结 14 1237
深忆病人
深忆病人 2020-11-28 02:23

I have an ASP.NET website written in C#.

On this site I need to automatically show a start page based on the user\'s location.

Can I get name of user\'s city

14条回答
  •  攒了一身酷
    2020-11-28 02:51

        public static string GetLocationIPAPI(string ipaddress)
        {
            try
            {
                IPDataIPAPI ipInfo = new IPDataIPAPI();
                string strResponse = new WebClient().DownloadString("http://ip-api.com/json/" + ipaddress);
                if (strResponse == null || strResponse == "") return "";
                ipInfo = JsonConvert.DeserializeObject(strResponse);
                if (ipInfo == null || ipInfo.status.ToLower().Trim() == "fail") return "";
                else return ipInfo.city + "; " + ipInfo.regionName + "; " + ipInfo.country + "; " + ipInfo.countryCode;
            }
            catch (Exception)
            {
                return "";
            }
        }
    
    public class IPDataIPINFO
    {
        public string ip { get; set; }
        public string city { get; set; }
        public string region { get; set; }
        public string country { get; set; }
        public string loc { get; set; }
        public string postal { get; set; }
        public int org { get; set; }
    
    }
    

    ==========================

        public static string GetLocationIPINFO(string ipaddress)
        {            
            try
            {
                IPDataIPINFO ipInfo = new IPDataIPINFO();
                string strResponse = new WebClient().DownloadString("http://ipinfo.io/" + ipaddress);
                if (strResponse == null || strResponse == "") return "";
                ipInfo = JsonConvert.DeserializeObject(strResponse);
                if (ipInfo == null || ipInfo.ip == null || ipInfo.ip == "") return "";
                else return ipInfo.city + "; " + ipInfo.region + "; " + ipInfo.country + "; " + ipInfo.postal;
            }
            catch (Exception)
            {
                return "";
            }
        }
    
    public class IPDataIPAPI
    {
        public string status { get; set; }
        public string country { get; set; }
        public string countryCode { get; set; }
        public string region { get; set; }
        public string regionName { get; set; }
        public string city { get; set; }
        public string zip { get; set; }
        public string lat { get; set; }
        public string lon { get; set; }
        public string timezone { get; set; }
        public string isp { get; set; }
        public string org { get; set; }
        public string @as { get; set; }
        public string query { get; set; }
    }
    

    ==============================

        private static string GetLocationIPSTACK(string ipaddress)
        {
            try
            {
                IPDataIPSTACK ipInfo = new IPDataIPSTACK();
                string strResponse = new WebClient().DownloadString("http://api.ipstack.com/" + ipaddress + "?access_key=XX384X1XX028XX1X66XXX4X04XXXX98X");
                if (strResponse == null || strResponse == "") return "";
                ipInfo = JsonConvert.DeserializeObject(strResponse);
                if (ipInfo == null || ipInfo.ip == null || ipInfo.ip == "") return "";
                else return ipInfo.city + "; " + ipInfo.region_name + "; " + ipInfo.country_name + "; " + ipInfo.zip;
            }
            catch (Exception)
            {
                return "";
            }
        }
    
    public class IPDataIPSTACK
    {
        public string ip { get; set; }
        public int city { get; set; }
        public string region_code { get; set; }
        public string region_name { get; set; }
        public string country_code { get; set; }
        public string country_name { get; set; }
        public string zip { get; set; }
    
    
    }
    

提交回复
热议问题