臨時更換網址: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;
}