Java access to the local IP and MAC address implementation of the detailed _java

Source: Internet
Author: User
Tags get ip http request

Java gets the IP and MAC address of this computer

Some machines have a number of virtual network cards, access to the IP address when there are some surprises, so need some authentication:

Get MAC address public static String getmacaddress () {try {enumeration<networkinterface> allnetinterfaces
      = Networkinterface.getnetworkinterfaces ();
      Byte[] mac = null; while (Allnetinterfaces.hasmoreelements ()) {NetworkInterface netinterface = (networkinterface) ALLNETINTERFACES.N
        Extelement ();
        if (Netinterface.isloopback () | | | netinterface.isvirtual () | |!netinterface.isup ()) {continue;
          else {mac = netinterface.gethardwareaddress ();
            if (Mac!= null) {StringBuilder SB = new StringBuilder (); for (int i = 0; i < mac.length i++) {sb.append (String.Format ("%02x%s", Mac[i), (I < mac.length-1) ?
            "-" : ""));
            } if (Sb.length () > 0) {return sb.tostring ();
    catch (Exception e) {_logger.error ("MAC address get Failed", e);
  Return ""; }//Get IP address public static StringGetipaddress () {try {enumeration<networkinterface> allnetinterfaces = Networkinterface.getnetworkinterfa
      CES ();
      inetaddress IP = null; while (Allnetinterfaces.hasmoreelements ()) {NetworkInterface netinterface = (networkinterface) ALLNETINTERFACES.N
        Extelement ();
        if (Netinterface.isloopback () | | | netinterface.isvirtual () | |!netinterface.isup ()) {continue;
          else {enumeration<inetaddress> addresses = netinterface.getinetaddresses ();
            while (Addresses.hasmoreelements ()) {IP = addresses.nextelement ();
            if (IP!= null && IP instanceof inet4address) {return ip.gethostaddress ();
    catch (Exception e) {_logger.error ("IP address acquisition failed", E);
  Return ""; }

In the above code

Netinterface.isloopback () | | Netinterface.isvirtual () | | !netinterface.isup ()

Can be very good to some non-physical network card or useless online filtering, and then take the online IPV4 address can be.

Here, there are some common:

1, get the current machine operating system

 Public final static String Win_os = "WINDOWS";
  Public final static String Mac_os = "MAC";
  Public final static String Linux_os = "LINUX";
  Public final static String Other_os = ' other ';

  public static String Getos () {
    if (systemutils.is_os_windows) {return
      win_os;
    }
    if (Systemutils.is_os_mac | | SYSTEMUTILS.IS_OS_MAC_OSX) {return
      mac_os;
    }
    if (Systemutils.is_os_unix) {return
      linux_os;
    }
    return other_os;
  }

2. Set HTTP Access Proxy

/**
   * Set HTTP Proxy */public
  static void Sethttpproxy () {
    Properties prop = System.getproperties ();
    Set the address of the proxy server to use for HTTP access
    prop.setproperty ("Http.proxyhost", http_proxy_host);
    Sets the port
    prop.setproperty ("Http.proxyport", Http_proxy_port) for HTTP access to the proxy server to use;
    To set up a host that does not need to be accessed through a proxy server, you can use the * wildcard character and multiple addresses to separate
    prop.setproperty ("Http.nonproxyhosts", Remoteconfig.proxt_filter_ DOMAIN);
  }
  
  /**
   * Remove HTTP Proxy */public
  static void Removehttpproxy () {
    Properties prop = System.getproperties ();
    prop.remove ("Http.proxyhost");
    Prop.remove ("Http.proxyport");
    Prop.remove ("http.nonproxyhosts");
  }

When the application is started, the set is done before accessing the HTTP request. Of course, http.nonproxyhosts can use no settings, which means that all HTTP requests go to proxy.

As for the HTTPS proxy, you can set it like this:

System.setproperty ("Https.proxyhost", "http_proxy_host");
System.setproperty ("Https.proxyport", "Http_proxy_port");

The above is the Java acquisition of native IP and Mac instances, the need for friends can refer to, thank you for the support of this site!

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.