如何通过IP地址了解国家和城市?

时间:2016-10-23 09:09:22

标签: c# ip

我有一个网站,用户可以向管理员发送消息,我想通过他的IP地址了解用户所在的国家和城市?

我的问题开始于我在服务器上发布我的网站,当任何用户发送消息时,我的功能为不同地方的所有用户选择相同的IP地址,并且具有不同的IP和我的代码:

   public string getPublicIP()        
   {
        string direction;

        WebRequest request =WebRequest.Create("http://checkip.dyndns.org/");

        WebResponse response = request.GetResponse();

    StreamReader stream = new StreamReader(response.GetResponseStream());

        direction = stream.ReadToEnd();

        stream.Close();

        response.Close(); 

        int first = direction.IndexOf("Address: ") + 9;

        int last = direction.LastIndexOf("");

        direction = direction.Substring(first, last - first);

        return direction.

        Replace("<html><head><title>Current IP Check</title></head>

        <body>Current IP Address: ", string.Empty).

        Replace("</body></html>", string.Empty);

    }

1 个答案:

答案 0 :(得分:0)

有多种在线服务可显示您的IP地址以及与该IP地址或该网站输入的任何其他IP地址相关联的城市,州或国家/地区。