js就两行代码就可以搞定:
<script language="javascript" type="text/javascript" src="http://fw.qq.com/ipaddress" charset="gb2312"></script>
<script>document.write("你的IP是:"+IPData[0]+",来自:"+IPData[2],IPData[3]);</script>
C#代码借助WebClient类来实现:
public static string GetUserLocation()
{
try
{
WebClient webGetting = new WebClient();
string userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
string ipQueryResult = webGetting.DownloadString("http://www.ip.cn/getip.php?action=queryip&ip_url=" + userIP);
string startString = @"来自:";
int startIndex = ipQueryResult.LastIndexOf(startString) + startString.Length;
int endIndex = ipQueryResult.LastIndexOf(@" ", startIndex);
return ipQueryResult.Substring(startIndex, ipQueryResult.Length - startIndex);
}
catch
{
return string.Empty;
}