c#获取公网的ip,本机ip 以及当前城市以及运营商

亲测,复制到项目中,可以直接使用。
调用:
//获取当前公网IP 以及地址
Dictionary<string, string> LocationName = new Dictionary<string, string>();
LocationName = GetLocation();

///
/// 获取页面html
///
/// 请求的地址
/// 编码方式
///
public static string GetHtml(string url, string encoding)
{
string pageHtml = string.Empty;
try
{
using (WebClient MyWebClient = new WebClient())
{
Encoding encode = Encoding.GetEncoding(encoding);
MyWebClient.Headers.Add(“User-Agent”, “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36”);
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
pageHtml = encode.GetString(pageData);
}
}
catch (Exception e)
{
return “访问 " + url + " 失败,请检查网络配置”;
}
return pageHtml;
}

    /// <summary>
    /// 从html中通过正则找到ipv4信息
    /// </summary>
    /// <param name="pageHtml"></param>
    /// <returns></returns>
    public static string GetGlobalIP()
    {
        string pageHtml = GetHtml("http://www.net.cn/static/customercare/yourip.asp", "gbk");
        //验证ipv4地址
        string reg = @"(?:(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))\.){3}(?:(25[0-5])|(2[0-4]\d)|((1\d{2})|([1-9]?\d)))";
        string ip = "none";
        Match m = Regex.Match(pageHtml, reg);
        if (m.Success)
        {
            ip = m.Value;
        }
        Console.WriteLine("公网ip:"+ip);
        return ip;
    }

    /// <summary>
    /// 根据IP地址获取城市信息
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string,string> GetLocation()
    {
        //string ip = "112.17.30.75";
        string ip = GetGlobalIP();
        string html = GetHtml("https://www.ip138.com/iplookup.asp?ip=" + ip + "&action=2", "gb2312");
        //Console.WriteLine(html);
        string pre = "var ip_result = {\"ASN归属地\":\"";
        int pos = html.IndexOf(pre);
        html = html.Substring(pos + pre.Length);
        html = html.Substring(0, html.IndexOf('"'));
        Console.WriteLine(html);
        // 变为"浙江省杭州市  移动 "
        string []res = html.Split(new char[] { '省', '市', ' ' },StringSplitOptions.RemoveEmptyEntries);
        foreach(var s in res)
        {
            Console.WriteLine(s);
        }

        var M = new Dictionary<string, string>();
        M["省"] = res[0];
        M["市"] = res[1];
        M["运营商"] = res[2];

        return M;
    }
    /// <summary>
    /// 获取本地IP地址
    /// </summary>
    /// <returns></returns>
    public static string GetIP()
    {
        string res = "";
        IPAddress[] addres = Dns.GetHostAddresses(Dns.GetHostName());
        foreach (var ip in addres)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                res += ip.ToString() + "  ";
            }
        }
        Console.WriteLine(res);
        return res;
    }

————————————————
感谢原文博主提供的方法
原文链接:https://blog.csdn.net/jk_chen_acmer/article/details/109531611

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值