首页 .NET/Web 如何获得外网IP地址 ASP.NET

如何获得外网IP地址 ASP.NET

作者:胡同里的砖头 围观群众:1247 更新于:2013-12-19

      调用GetIP getip = Get();
        Label1.Text = getip.Ip;
        Label2.Text = getip.Country;

public class GetIP
    {
        private string _ip;//IP

        public string Ip
        {
            get { return _ip; }
            set { _ip = value; }
        }
        private string country;//归属地

        public string Country
        {
            get { return country; }
            set { country = value; }
        }
    }

    public static GetIP Get()
    {
        Uri uri = new Uri(" http://www.ip.cn/getip.php?action=getip&ip_url=");
        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = 0;
        req.CookieContainer = new System.Net.CookieContainer();
        req.GetRequestStream().Write(new byte[0], 0, 0);
        System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
        System.IO.StreamReader rs = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
        string s = rs.ReadToEnd();
        rs.Close();
        req.Abort();
        res.Close();
        GetIP getip = new GetIP();
        int start = s.IndexOf(">");
        int end = s.IndexOf("</");
        getip.Ip = s.Substring(start + 1, end - start - 1);
        getip.Country = s.Substring(s.IndexOf(":") + 1, s.Length - s.IndexOf(":") - 1);
        return getip;
    }

  • 本文标题: 如何获得外网IP地址 ASP.NET
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8