利用WebClient取得IP位址所在區域
.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="IpQuery.aspx.cs" Inherits="IpQuery" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Query" /><br />
13 <asp:Label ID="Label1" runat="server"></asp:Label></div>
14 </form>
15 </body>
16 </html>
17
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>未命名頁面</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Query" /><br />
13 <asp:Label ID="Label1" runat="server"></asp:Label></div>
14 </form>
15 </body>
16 </html>
17
.cs
01
using System;
02
using System.Data;
03
using System.Configuration;
04
using System.Collections;
05
using System.Web;
06
using System.Web.Security;
07
using System.Web.UI;
08
using System.Web.UI.WebControls;
09
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Net;
12
using System.IO;
13
14
public partial class IpQuery : System.Web.UI.Page
15
{
16
protected void Page_Load(object sender, EventArgs e)
17
{
18
19
}
20
protected void Button1_Click(object sender, EventArgs e)
21
{
22
WebClient client = new WebClient();
23
24 //從http://www.dl.ro/index/utils.country/txtIp/xxx.xxx.xxx.xxx取出html碼,再取得html碼裡區域的資訊
25
Stream data = client.OpenRead("http://www.dl.ro/index/utils.country/txtIp/" + this.TextBox1.Text);
26
StreamReader reader = new StreamReader(data);
27
string s = reader.ReadToEnd();
28
29 //查詢結果
30
this.Label1.Text = SplitStr(s);
31
}
32
33 //取出html語法,取出區域資訊
34
protected string SplitStr(string str)
35
{
36
37 int start = str.IndexOf(@"Indice tara (ISO 2 caractere)");
38
39 int end = str.IndexOf(@"Tara (descriere lunga)");
40
41 if (start > 0 & end > 0)
42
{
43
string ss = str.Substring(start + 29, end - start - 55);
44
45
string[] result = ss.Split(new string[] { "</td><td>", "</td></tr>" }, StringSplitOptions.RemoveEmptyEntries);
46
47 return result[0];
48
}
49
else
50
{
51
return "輸入IP有誤!";
52
}
53
}
54
}

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24 //從http://www.dl.ro/index/utils.country/txtIp/xxx.xxx.xxx.xxx取出html碼,再取得html碼裡區域的資訊
25

26

27

28

29 //查詢結果
30

31

32

33 //取出html語法,取出區域資訊
34

35

36

37 int start = str.IndexOf(@"Indice tara (ISO 2 caractere)");
38

39 int end = str.IndexOf(@"Tara (descriere lunga)");
40

41 if (start > 0 & end > 0)
42

43

44

45

46

47 return result[0];
48

49

50

51

52

53

54
