首页 新闻 搜索 专区 学院

求一个可以获得外网ip的代码

0
[已关闭问题] 关闭于 2009-07-07 17:04

求一个可以获得外网ip的代码

 

牛牛博客的主页 牛牛博客 | 菜鸟二级 | 园豆:305
提问于:2009-07-03 20:26
< >
分享
所有回答(1)
0

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace ipip
{

static string netIP()
   {
    Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");//查本机网络IP的网页
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = 0;
    req.CookieContainer = new CookieContainer();
    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();
    rs.Close();
    req.Abort();
    res.Close();
    Match m = Regex.Match(s, @"IP:\[(?<IP>[0-9\.]*)\]");
    if (m.Success) return m.Groups["IP"].Value;
    string strnetIP= string.Empty;
    return strnetIP;
   }

}

Jaryleely | 园豆:367 (菜鸟二级) | 2009-07-03 22:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册