tnblog
首页
视频
资源
登录

C# 获取真实IP地址

3475人阅读 2020/4/30 9:32 总访问:85835 评论:5 收藏:0 手机
分类: .NET Core
//192.168开头的IP是一个代理Ip并不是你真实的IP

  public string GetRelayIP()
        {
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            byte[] pageData = myWebClient.DownloadData("https://www.ip38.com/");
            string pageHtml = Encoding.UTF8.GetString(pageData);

            pageHtml = pageHtml.Replace('\'', '\"');
            pageHtml = pageHtml.Replace("ip", "\"ip\"");
            pageHtml = pageHtml.Replace("address", "\"address\"");

            //根据返回会来的html进行切割 定位到ip
            string pram = "<a href=/\"ip\".php?\"ip\"";
            string[] arr = pageHtml.Split(pram.ToCharArray());

            string strtempa = "=";
            string strtempb = ">";

            string ipstr = arr[1];

            int IndexofA = ipstr.IndexOf(strtempa);
            int IndexofB = ipstr.IndexOf(strtempb);
            string ip = ipstr.Substring(IndexofA + 1, IndexofB - IndexofA - 1);

            return ip;
        }


评价
我要变强不变秃
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术