zoukankan      html  css  js  c++  java
  • 获取用户电脑的上网IP地址

               在项目中经常要获取用户的上网的IP地址,如何获取用户的IP地址,方法很多,现在介绍以下2种。

    /// <summary>
            /// 获取本机在局域网的IP地址
            /// </summary>
            /// <returns></returns>
            private string GetLocalIPAddress()
            {
                System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
                string strNativeIP = "";
                string strServerIP = "";
                if (addressList.Length > 1)
                {
                    strNativeIP = addressList[0].ToString();
                    strServerIP = addressList[1].ToString();
                }
                else if(addressList.Length==1)
                {
                    strServerIP = addressList[0].ToString();
                }
                return strServerIP;
            }

    另外一种就是抓取网页中查询到的上网地址的IP来实现的。实现如下:

    /// <summary>
            /// 获取本机的上网IP
            /// </summary>
            /// <returns></returns>
            private string GetConnectNetAddress()
            {
                string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址
                Uri uri = new Uri(strUrl);
                WebRequest webreq = WebRequest.Create(uri);
                Stream s = webreq.GetResponse().GetResponseStream();
                StreamReader sr = new StreamReader(s, Encoding.Default);
                string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
                int i = all.IndexOf("[") + 1;
                string tempip = all.Substring(i, 15);
                string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", "");
                return ip;
            }


  • 相关阅读:
    Windows7 如何添加excel,word到鼠标右键
    Java程序安装失败
    交换机
    Hbase
    Hive
    Hdoop
    PL/SQL连不上,报 ORA-12170:TNS 连接超时
    Error in invoking target 'mkldflags ntcontab.o nnfgt.o' of mkdefile '/u01/app/oracle/product/11.2.0
    用js 的for循环打印三角形,提取水仙花数,求本月多少天
    JS循环、数组与练习题
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2482116.html
Copyright © 2011-2022 走看看