我想通过程序访问www.ip.cn,读取本机的ip信息。
我找到了一段源码,发现能用。
但是我没学javascript,谁能帮我逐步分析一下?
一定要详细啊
static string GetIP()
{
    Uri             uri = new Uri("http://www.ikaka.com/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("GB18030"));
    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;
    return string.Empty;
}

解决方案 »

  1.   

    自己顶一下~
    以www.ip.cn为例,打开网页的源文件,是找不到 ip地址的。
    我想应该是 js 函数返回值填进去的吧?
    用C#怎么获得这个函数的返回值?源代码摘自这里:
    http://helloworld84.cnblogs.com/archive/2005/08/18/218104.html
      

  2.   

    没有一行javascript代码!它将string s = rs.ReadToEnd();所得到的变量s,使用正则表达式抽取出符合
         IP:\[(?<IP>[0-9\.]*)\]
    的部分。其中的问号?<IP>表示正则表达式使用关键字“IP”来保存这个圆括号组()所匹配的字符串,把它作为得到的ip字符串返回。
      

  3.   

    是我自己太多心了?
     req.Method = "POST";这是做什么的?
      

  4.   

    req.Method = "POST";这是做什么的?
      

  5.   

    代表你的提交方式是post提交,一般都是post
      

  6.   

    个人感觉还不如访问本地的路由表,找到0.0.0.0缺省路由对应的那个接口的ip,一般情况下都是外网ip。