Error in resolving domain name in program

In the project, the data is sent to the third party through the webservice interface. The local configuration is the domain name given by the third party. When the interface is called:
java.net.UnknownHostException: XXXX.XXXX.com
              ..... The
call to the third-party interface keeps failing.

Check the information on the Internet, the reasons are as follows:
The host name and the local loop address in the host file do not match, so an error is reported.

Solution:
(1) You need to add the resolved IP address of the domain name to the hosts file. The windows server configuration is as follows:
     open windows/system32/driver/etc/hosts, add the following content: domain name resolution ip domain name
(2) use the getByName method in etAddress in java, which can be obtained through the incoming string (domain name) field The value of "domain name/IP", and then the IP address can be obtained through the getHostAddress() method.
Method 1: Get only one ip address under the domain name (the first one)
   InetAddress address = null;
    try {
    	String name = "www.baidu.com";
        address = InetAddress.getByName(name);
    } catch (UnknownHostException e) {
       e.printStackTrace ();
    }
     System.out.println("ip: " + address.getHostAddress().toString());

Method 2: Get all IP addresses under the domain name
  
   InetAddress[] addresses;
	try {
                String name = "www.baidu.com";
		addresses = InetAddress.getAllByName(name);
		System.out.println(addresses.length);
		for (InetAddress addr : addresses) {
		     System.out.println(addr);
		}
	} catch (UnknownHostException e) {
		e.printStackTrace ();
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326446026&siteId=291194637
Recommended