111,098
社区成员




private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = this.GetOuterIP();
}
//----
public string GetOuterIP()
{
string patt = @"IP: \[(?<IP>[0-9\.]*)\]";
string url = "http://www.baidu.com/";
string ip = GetPage(url);
textBox3.Text = ip;
return Regex.Match(ip, patt).Groups["IP"].Value;
}
//获取网页的HTML内容
static string GetPage(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
}
catch (System.Exception e)
{
return e.Message;
}
finally
{
req.Abort();
}
}
//获取外网IP
public string getNetIP()
{
try
{
Uri uri = new Uri("http://www.3322.org/dyndns/getip");//查本机网络IP的网页
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Timeout = 10000;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();//伪造cookie
req.GetRequestStream().Write(new byte[0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
Match m = Regex.Match(s, @"[0-9\.]*");
rs.Close();
res.Close();
req.Abort();
if (m.Success)
return m.Value;
}
catch
{
return "获取失败";
}
return "获取失败";
}
//获取外网IP
public IPAddress GetMyIP()
{
//下载数据
WebClient client = new WebClient();
byte[] bytRecv = client.DownloadData("http://www.ip138.com/");
string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytRecv);
//提取信息
string regexStr = @"(?<=您的IP地址是:)[^<]+";
string myip = Regex.Match(str, regexStr).ToString();
return IPAddress.Parse(myip);
}
//获取本地IP
public string[] GetMyHomeIp()
{
string homename = Dns.GetHostName();
IPAddress[] ipa = Dns.GetHostAddresses(homename);
string[] IPaddrs = new string[ipa.Length];
for (int i = 0; i < ipa.Length; i++)
{
IPaddrs[i] = ipa[i].ToString();
}
return IPaddrs;
}
//Get
public void GetIP()
{
string homename = Dns.GetHostName();
IPHostEntry homeIplist = Dns.GetHostByName(homename);
IPAddress [] IpList = homeIplist.AddressList;
for (int i = 0; i < IpList.Length; i++)
{
Debug.WriteLine(IpList[i].ToString());
Debug.WriteLine(IpList[i].Address);
Debug.WriteLine(IpList[i].AddressFamily);
}
}