zoukankan      html  css  js  c++  java
  • <转载>C#如何获取本机网络ip地址

    C#如何获取本机网络ip

    有几种方法获取本机局域网地址和网络地址。

    1.  获取本机局域网地址:

    使用IPHostEntry

             

    2. 获取本机网络ip地址

    通过向网站http://www.ip138.com/ip2city.asp发送webrequest,分析返回的数据流

               try            {                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("<","");     //去除杂项找出ip                return ip;            }

    3. 获取本机网络ip地址和城市地址,webservices方式

    利用网站webxml提供的获取ip的web services,在引用网站后取得ip地址

                try             {                 webxmlIPservice.IpAddressSearchWebService WebIP = new webxmlIPservice.IpAddressSearchWebService();                 string[] strIP = WebIP.getGeoIPContext();                 return strIP[1];    //返回的数组0为ip地址,数组1为城市             }

    以下左下角为利用第3种方法获取的本地城市信息:

  • 相关阅读:
    MySQL主从复制搭建
    CSS基本样式简单介绍
    前端代码编码规范
    Markdown语法简单介绍
    Java API 操作Redis
    Java API 操作Zookeeper
    MySQL优化
    ES 可视化工具
    消息队列MQ
    Docker基础
  • 原文地址:https://www.cnblogs.com/ChangTan/p/2601801.html
Copyright © 2011-2022 走看看