在局域网内获取本机的外部IP
“ipconfig.exe /all”在局域网中只能获取内部IP, 无法获取本机的外部IP, Dns.Resolve()也一样。
经研究, 在局域网中, 仅凭本机的力量是无法获得本机的外部IP的, 必须登录到外网某个服务器, 那个服务器就知道你的IP, 然后你再取回来。
1
using System;
2
using System.IO;
3
using System.Net;
4
using System.Text.RegularExpressions;
5![](/Images/OutliningIndicators/None.gif)
6
class Test
7
{
8
// 获取网页的HTML内容
9
static string GetPage(string url)
10
{
11
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
12
try
13
{
14
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
15
{
16
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
17
{
18
return sr.ReadToEnd();
19
}
20
}
21
}
22
catch (System.Exception e)
23
{
24
return e.Message;
25
}
26
finally
27
{
28
req.Abort();
29
}
30
}
31![](/Images/OutliningIndicators/InBlock.gif)
32
// 通过外部网站得到本机的外部IP
33
static string GetOuterIP()
34
{
35
string patt = @"IP: \[(?<IP>[0-9\.]*)\]";
36
string url = "http://www.skyiv.com/info";
37
return Regex.Match(GetPage(url), patt).Groups["IP"].Value;
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40
static void Main()
41
{
42
Console.WriteLine("Inner IP:");
43
foreach (IPAddress ip in Dns.Resolve(Dns.GetHostName()).AddressList)
44
{
45
Console.WriteLine(ip);
46
}
47
Console.WriteLine();
48
Console.WriteLine("Outer IP:");
49
Console.WriteLine(GetOuterIP());
50
}
51
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)