嘿,我的想法很簡單,我會從Utrace API的IP地址(例如代理服務器)獲取位置信息。使用Utrace獲取位置後解析XML
看看這段代碼(感謝谷歌)
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;
}
OK但是從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有限公司& Co. KG的貝肖芬
問候和感謝
非常感謝你非常感謝你這是一個奇妙的解決方案,謝謝 –