大家好,我的想法很简单,我将使用 Utrace API 从 IP 地址(例如代理)获取位置信息。
看看这段代码(感谢谷歌)
public static string GetLocation(string IP)
{
var location = "";
List<string> HTML_code = new List<string>();
WebRequest request = WebRequest.Create("http://xml.utrace.de/?query=" + IP);
using (WebResponse response = request.GetResponse())
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
string line;
while ((line = stream.ReadLine()) != null)
{
HTML_code.Add(line);
}
}
location = (HTML_code[296].Replace("<td><font size=\"-1\">", "")).Replace("</font></td>", "");
return location;
}
好的,但是 utrace API 的输出如下:
<?xml version="1.0" encoding="iso-8859-1"?>
<results>
<result>
<ip>188.40.16.134</ip>
<host></host>
<isp>Hetzner Online AG Pagedesign</isp>
<org>Hetzner Online AG Pagedesign</org>
<region>Koblenz</region>
<countrycode>DE</countrycode>
<latitude>50.349998474121</latitude>
<longitude>7.5999999046326</longitude>
<queries>8</queries>
</result>
</results>
我的 XML 技能不是最好的,希望你们能帮助我编辑这一行:
location = (HTML_code[296].Replace("<td><font size=\"-1\">", "")).Replace("</font></td>", "");
我会这样的输出:
Hetzner Online AG Pagedesign : Koblenz
并不是
<?xml version="1.0" encoding="iso-8859-1"?>
<results>
<result>
<ip>188.40.16.134</ip>
<host></host>
<isp>Hetzner Online AG Pagedesign</isp>
<org>Hetzner Online AG Pagedesign</org>
<region>Koblenz</region>
<countrycode>DE</countrycode>
<latitude>50.349998474121</latitude>
<longitude>7.5999999046326</longitude>
<queries>8</queries>
</result>
</results>
在此先感谢您的帮助
编辑:
我的新代码如下:
public static void getloc(string ip)
{
var location = "";
var wc = new WebClient();
location = wc.DownloadString("http://xml.utrace.de/?query=" + ip);
location = location.Replace("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>", "").Replace("<results>", "").Replace("<result>", "")
.Replace("<ip></ip>", "").Replace("<org></org>", "").Replace("<latitude></latitude>", "").Replace("<longitude></longitude>", "").Replace("<queries>*</queries>", "")
.Replace("</result>", "").Replace("</results>", "");
Console.WriteLine(location);
}
输出是:
<ip>212.19.62.76</ip>
<host>1</host>
<isp>Plus.line AG</isp>
<org>ANW GmbH & Co. KG</org>
<region>Bechhofen</region>
<countrycode>DE</countrycode>
<latitude>49.150001525879</latitude>
<longitude>10.550000190735</longitude>
<queries>6</queries>
如何获得像 Plus.line AG ANW GmbH & Co. KG Bechhofen 这样的输出
问候和感谢