如何获取外网Ip呢, 终于找到方法了

时间:2024-02-24 07:12:26

临时更换网址:http://20140507.ip138.com/ic.asp

这个网址能同时获取ip和城市名字

上面的网址如何来的呢,其实很简单,随便打开一个获取Ip的网站,比如http://www.ip138.com/,会发现主页上有你的Ip地址和你的来源,那么这个获取到的肯定来自于一段程序,查看源代码后发现,果然,代码如下

 

<br/><table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
  <tr><td align="center"><h3>www.ip138.com IP查询(搜索IP地址的地理位置)</h3></td></tr>
  <tr>
  <td height="30" align="center" valign="top"><iframe src="http://20140507.ip138.com/ic.asp" rel="nofollow" frameborder="0" scrolling="no" width="100%" height="100%"></iframe></td>
  </tr>
  <tr>
  </tr>

 

看到iframe src="http://20140507.ip138.com/ic.asp",就这个,以后再改动,就这样看就行了,无论哪个网站,应该都是类似的做法

 

 

获取ip的C#代码:

 /// <summary>
        /// 获取客户端的 IP 信息
        /// </summary>
        /// <returns></returns>
        public static string GetUserIP( )
        {
            if ( HttpContext.Current == null )
            {
                return string.Empty;
            }
            string ipval = string.Empty;
            ipval = HttpContext.Current.Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ];
            switch ( ipval )
            {
                case null:
                case "":
                    ipval = HttpContext.Current.Request.ServerVariables[ "REMOTE_ADDR" ];
                    break;
            }
            if ( ( ipval == null ) || ( ipval == string.Empty ) )
            {
                ipval = HttpContext.Current.Request.UserHostAddress;
            }
            if ( !( ( ( ipval != null ) && ( ipval != string.Empty ) ) && Validate.IsIP( ipval ) ) )
            {
                return "0.0.0.0";
            }
            return ipval;
        }