From: CikerBlog
Form圖:
完成圖:
只貼擷取IP部分的代碼:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Dns.Resolve(Dns.GetHostName()).AddressList.GetValue(0).ToString(); //內網IP
textBox2.Text = GetIP(); //外網IP
}
static string GetIP()
{
Uri uri = new Uri("http://www.ciker.net/ip/index.asp");
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new System.Net.CookieContainer();
req.GetRequestStream().Write(new byte[0], 0, 0);
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"您的IP:(?<IP>[0-9\.]*)");
if (m.Success)
{
return m.Groups["IP"].Value;
}
else
{
return "Failed..";
}
}