C#使用有道ip地址查詢介面方法執行個體詳解
本文執行個體講述了C#使用有道ip地址查詢介面方法。分享給大家供大家參考。具體實現方法如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
#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資料格式:
?
|
1
2
3
4
5
6
7 |
<?xml version="1.0" encoding="gbk" ?>
<smartresult>
<product type="ip">
<ip>60.223.233.226</ip>
<location>山西省朔州市 網通</location>
</product>
</smartresult>
|
希望本文所述對大家的C#程式設計有所協助。