C # Get native intranet extranet IP

Source: Internet
Author: User

Using system.net;# region get internal, external network ip///<summary>///get local IP address, take precedence of intranet ip///</summary>public static String    Getlocalip () {string[] Ips = getlocalipaddress (); foreach (String IP in Ips) if (IP. StartsWith ("10.80."))    return IP; foreach (String IP in Ips) if (IP. Contains ("."))    return IP; return "127.0.0.1";} <summary>///gets the local IP address.                    Multiple ip///</summary>public static string[] Getlocalipaddress () {String hostName = Dns.gethostname (); Get host name ipaddress[] addresses = dns.gethostaddresses (hostName); Resolve host IP address string[] IP = new string[addresses.             Length]; Convert to string form for (int i = 0; i < addresses. Length; i++) Ip[i] = Addresses[i].    ToString (); return IP;} <summary>///get the Extranet IP address///</summary>public static string Getextenalipaddress_0 () {String ip = "not acquired to extranet IP"    ;        try {//Get native IP data from the URL System.Net.WebClient client = new System.Net.WebClient (); Client. Encoding = System.Text.EncodIng.        Default; String str = client.        Downloadstring ("http://1111.ip138.com/ic.asp"); Client.        Dispose (); Extract the extranet IP data [218.104.71.178] int i1 = str. IndexOf ("["), i2 = str.        IndexOf ("]"); IP = str.    Substring (I1 + 1, i2-1-i1); } catch (Exception) {} return IP;} <summary>///get the Extranet IP address///</summary>public static string Getextenalipaddress () {String url = "Http://hij    Oyusers.joymeng.com:8100/test/getnamebyotherip ";    string IP = "not acquired to extranet IP";        try {//Get native IP data from the URL System.Net.WebClient client = new System.Net.WebClient (); Client.        Encoding = System.Text.Encoding.Default; String str = client.        downloadstring (URL); Client.        Dispose (); if (!str.        Equals ("")) IP = str;    else IP = Getextenalipaddress_0 (); } catch (Exception) {} return IP;} # endregion


C # Get native intranet extranet IP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.