本文實例講述了C#使用有道ip地址查詢接口方法。分享給大家供大家參考。具體實現方法如下:
#region 讀取http://www.yodao.com接口IP地址/// <summary>/// 讀取http://www.yodao.com接口IP地址/// </summary>public static string GetstringIpAddress(string strIP)//strIP為IP{ string sURL = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q="+strIP+""; //youdao的URL string stringIpAddress = ""; using (XmlReader read = XmlReader.Create(sURL)) //獲取youdao返回的xml格式文件內容 { while (read.Read()) { switch (read.NodeType) { case XmlNodeType.Text://取xml格式文件當中的文本內容 if (string.Format("{0}", read.Value).ToString().Trim() != strIP) //youdao返回的xml格式文件內容一個是IP, //另一個是IP地址,如果不是IP那么就是IP地址 { stringIpAddress=string.Format("{0}", read.Value).ToString().Trim();//賦值 } break; //other } } } return stringIpAddress;}
返回的xml數據格式:
<?xml version="1.0" encoding="gbk" ?> <smartresult><product type="ip"><ip>60.223.233.226</ip> <location>山西省朔州市 網通</location> </product></smartresult>
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答