获取本机外网ip和内网ip
2016-06-28 18:42
190 查看
获取本机外网ip
获取本机内网ip
//获取本机的公网IP public static string GetIP() { string tempip = ""; try { WebRequest request = WebRequest.Create("http://ip.qq.com/"); request.Timeout = 10000; WebResponse response = request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default); string htmlinfo = sr.ReadToEnd(); //匹配IP的正则表达式 Regex r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None); Match mc = r.Match(htmlinfo); //获取匹配到的IP tempip = mc.Groups[0].Value; resStream.Close(); sr.Close(); } catch (Exception err) { tempip = err.Message; } return tempip; }
获取本机内网ip
//获取内网IP private string GetInternalIP() { IPHostEntry host; string localIP = "?"; host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily.ToString() == "InterNetwork") { localIP = ip.ToString(); break; } } return localIP; }
相关文章推荐
- NGUI 指定视口大小
- 协方差的意义和解释(目前为止我看到的最为通俗易懂的文章)
- 嵌入式学习-uboot-lesson7-内存初始化
- 新浪OAuth网站登陆连接,请求access_token时遇到21323 miss client id or secret 的解决方案
- 基于RealSense的坐姿检测技术
- 获取网站Web服务器类型的办法
- C#实现上传图片
- Python中的迭代器与生成器高级用法解析
- 配置TOMACAT
- iOS开发 UITabBar角标 红点形式 (tabBarItem.badgeValue)
- http与http2.0
- 导入excel的步骤分析
- mysql生成订单编号函数
- Java三大特性之---继承
- python学习笔记之面向对象编程特性(二)
- Spark定制班第33课:Kafka内核再解密
- Xml和Json相互转换的java
- 一些备份
- PotPlayer清除流氓和升级
- RPM包的制作