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;
            }

 

相关文章:

  • 2021-07-26
  • 2021-12-05
  • 2021-12-10
  • 2021-11-15
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2021-12-15
相关资源
相似解决方案