C# winfrom获取外网IP地址和物理地址

因项目需要,老大让做一个类似QQ那样能知道你的登陆地点的东西。

  这下慌啊,。后来找到一个网站 www.ip138.com

  进去看了一下惊喜啊羡慕,我要的东西里面都有。 可是,怎么样能将我想要的物理地址拿出来,放在我自己的项目中呢。

   下面的代码能解释一切

       try
            {
                string strUrl = "http://city.ip138.com/city.asp"; //获得IP的网址了
                Uri uri = new Uri(strUrl);
                System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
                System.IO.Stream s = wr.GetResponse().GetResponseStream();
                System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.Default);
                string all = sr.ReadToEnd(); //读取网站的数据

                int i = all.IndexOf("[") + 1;
                string tempip = all.Substring(i, 15);
                ip = tempip.Replace("]", "").Replace(" ", "");//找出公网IP
                int ii=all.LastIndexOf(":") + 1;
                Address = all.Substring(ii, all.LastIndexOf(" ") - all.LastIndexOf(":") - 1);//找出物理地址,如:江苏省苏州市
                label10.Text = "您当前所在的位置是:" + Address;
            }
            catch (Exception ex)
            {
            }

   问题一下子解决,唉,终于松了口气,那个叫心情舒畅啊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值