如何在c#中使用hostip.info服务?

如何在c#中使用hostip.info服务?,c#,asp.net,C#,Asp.net,我有一个名为getIP()的方法,它以字符串形式返回客户端ip 如何使用此IP获取使用服务的客户端的位置 这就是我显示客户端IP地址的方式 string IP = getIP(); lblIPAddress.Text = "IP " + IP; 如何将客户的位置包括在中 i.e. lblIPAddress.Text = "IP " + IP+ "location" ;) 在下面,您可以找到一个非常简单的片段,我不久前曾使用它从该API的XML端点获取数据(我相信API没有任何更改,所以它

我有一个名为getIP()的方法,它以字符串形式返回客户端ip

如何使用此IP获取使用服务的客户端的位置

这就是我显示客户端IP地址的方式

string IP = getIP();
lblIPAddress.Text = "IP " + IP;
如何将客户的位置包括在中

i.e. lblIPAddress.Text = "IP " + IP+ "location" ;)  

在下面,您可以找到一个非常简单的片段,我不久前曾使用它从该API的XML端点获取数据(我相信API没有任何更改,所以它仍然可以工作):

这段代码当然可以改进,但我相信这是一个很好的起点

string city;
string country;
string countryCode;
decimal longitude;
decimal latitude;

XmlTextReader hostIPInfoReader = new XmlTextReader("http://api.hostip.info/?ip=" + IP);
while (hostIPInfoReader.Read()) {
    if (hostIPInfoReader.IsStartElement()) {
        if (hostIPInfoReader.Name == "gml:name")
            city = hostIPInfoReader.ReadString();

        if (hostIPInfoReader.Name == "countryName")
            country = hostIPInfoReader.ReadString();

        if (hostIPInfoReader.Name == "countryAbbrev")
            countryCode = hostIPInfoReader.ReadString();

        if (hostIPInfoReader.Name == "gml:coordinates") {
            string[] coordinates = hostIPInfoReader.ReadString().Split(new char[] { ',' });
            longitude = decimal.Parse(coordinates[0]);
            latitude = decimal.Parse(coordinates[1]);
        }
    }
}