C#使用有道ip地址查詢接口方法實例詳解
更新時間:2015年05月25日 10:33:05 作者:陌香
這篇文章主要介紹了C#使用有道ip地址查詢接口方法,實例分析了有道IP地址查詢接口的使用方法與數(shù)據(jù)返回格式,需要的朋友可以參考下
本文實例講述了C#使用有道ip地址查詢接口方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
#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格式文件內(nèi)容
{
while (read.Read())
{
switch (read.NodeType)
{
case XmlNodeType.Text://取xml格式文件當中的文本內(nèi)容
if (string.Format("{0}", read.Value).ToString().Trim() != strIP)
//youdao返回的xml格式文件內(nèi)容一個是IP,
//另一個是IP地址,如果不是IP那么就是IP地址
{
stringIpAddress=string.Format("{0}", read.Value).ToString().Trim();//賦值
}
break;
//other
}
}
}
return stringIpAddress;
}
返回的xml數(shù)據(jù)格式:
<?xml version="1.0" encoding="gbk" ?> <smartresult> <product type="ip"> <ip>60.223.233.226</ip> <location>山西省朔州市 網(wǎng)通</location> </product> </smartresult>
希望本文所述對大家的C#程序設(shè)計有所幫助。

