コード例 #1
0
        public static string GetNetIP()
        {
            string str = "";

            try
            {
                Stream       responseStream = WebRequest.Create("http://city.ip138.com/ip2city.asp").GetResponse().GetResponseStream();
                StreamReader streamReader   = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
                string       end            = streamReader.ReadToEnd();
                int          startIndex     = end.IndexOf("[") + 1;
                int          num            = end.IndexOf("]", startIndex);
                str = end.Substring(startIndex, num - startIndex);
                streamReader.Close();
                responseStream.Close();
            }
            catch
            {
                if (Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length > 1)
                {
                    str = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
                }
                if (string.IsNullOrEmpty(str))
                {
                    return(IPHelp.GetIP());
                }
            }
            return(str);
        }
コード例 #2
0
        public static string GetNetIP()
        {
            string text = "";

            try
            {
                WebRequest   webRequest     = WebRequest.Create("http://city.ip138.com/ip2city.asp");
                Stream       responseStream = webRequest.GetResponse().GetResponseStream();
                StreamReader streamReader   = new StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
                string       text2          = streamReader.ReadToEnd();
                int          num            = text2.IndexOf("[") + 1;
                int          num2           = text2.IndexOf("]", num);
                text = text2.Substring(num, num2 - num);
                streamReader.Close();
                responseStream.Close();
            }
            catch
            {
                if (Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length > 1)
                {
                    text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[1].ToString();
                }
                if (string.IsNullOrEmpty(text))
                {
                    return(IPHelp.GetIP());
                }
            }
            return(text);
        }