Get user location by IP address

前端 未结 14 1274
深忆病人
深忆病人 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 03:00

    Return country

    static public string GetCountry()
    {
        return new WebClient().DownloadString("http://api.hostip.info/country.php");
    }
    

    Usage:

    Console.WriteLine(GetCountry()); // will return short code for your country
    

    Return info

    static public string GetInfo()
    {
        return new WebClient().DownloadString("http://api.hostip.info/get_json.php");
    }
    

    Usage:

    Console.WriteLine(GetInfo()); 
    // Example:
    // {
    //    "country_name":"COUNTRY NAME",
    //    "country_code":"COUNTRY CODE",
    //    "city":"City",
    //    "ip":"XX.XXX.XX.XXX"
    // }
    

提交回复
热议问题