Java网络编程(二)IP、URL和HTTP

  一、IP

  InetAddress类有一些静态工厂方法,可以连接到DNS服务器来解析主机名。

  示例1:InetAddress address = InetAddress.getByName("www.baidu.com");会建立一个与本地DNS服务器的一个连接,来查找域名和IP地址。

package aboutIP;

import java.net.*;

public class GetIPByName {

  public static void main (String[] args) {
    try {
      InetAddress address = InetAddress.getByName("www.baidu.com");
      System.out.println(address);
    } catch (UnknownHostException ex) {
      System.out.println("Could not find www.baidu.com");
    }
  }
}
www.baidu.com/111.206.223.205

  示例2:查找本地主机的IP地址。

import java.net.*;

public class MyAddress {

  public static void main (String[] args) {
    try {
      InetAddress address = InetAddress.getLocalHost();
      System.out.println(address);
    } catch (UnknownHostException ex) {
      System.out.println("Could not find this computer's address.");
    }
  }
}
BigjunOba/192.168.131.1

  示例3:给定IP地址,查找主机名

import java.net.*;

public class ReverseTest {

  public static void main (String[] args) throws UnknownHostException {
    InetAddress ia = InetAddress.getByName("192.168.131.1");
    System.out.println(ia.getCanonicalHostName());
  }
}
BigjunOba

  示例4:确定IP地址版本。

import java.net.*;

public class AddressTests {

  public static int getVersion(InetAddress ia) {
    byte[] address = ia.getAddress();
    if (address.length == 4) return 4;
    else if (address.length == 16) return 6;
    else return -1;
  }
}

  示例5:测试IP地址的类型

import java.net.*;

public class IPCharacteristics {

  public static void main(String[] args) {
  
    try {
      InetAddress address = InetAddress.getByName(args[0]);
      
      if (address.isAnyLocalAddress()) {
        System.out.println(address + " is a wildcard address.");
      }
      if (address.isLoopbackAddress()) {
        System.out.println(address + " is loopback address.");
      }
      if (address.isLinkLocalAddress()) {
        System.out.println(address + " is a link-local address.");
      } else if (address.isSiteLocalAddress()) {
        System.out.println(address + " is a site-local address.");
      } else {
        System.out.println(address + " is a global address.");
      }
      
      if (address.isMulticastAddress()) {
        if (address.isMCGlobal()) {
          System.out.println(address + " is a global multicast address.");
        } else if (address.isMCOrgLocal()) {
          System.out.println(address  + " is an organization wide multicast address.");
        } else if (address.isMCSiteLocal()) {
          System.out.println(address + " is a site wide multicast address.");
        } else if (address.isMCLinkLocal()) {
          System.out.println(address + " is a subnet wide multicast address.");
        } else if (address.isMCNodeLocal()) {
          System.out.println(address + " is an interface-local multicast address.");
        } else {
          System.out.println(address + " is an unknown multicast address type.");
        }   
      } else {
        System.out.println(address + " is a unicast address.");          
      }
    } catch (UnknownHostException ex) {
      System.err.println("Could not resolve " + args[0]);
    }   
  }
}
/127.0.0.1 is loopback address.
/127.0.0.1 is a global address.
/127.0.0.1 is a unicast address.
/192.168.131.1 is a site-local address.
/192.168.131.1 is a unicast address.
www.baidu.com/111.206.223.205 is a global address.
www.baidu.com/111.206.223.205 is a unicast address.

  示例6:测试可达性

import java.net.*;

public class IBiblioAddress {

  public static void main (String args[]) {
    try {
      InetAddress ibiblio = InetAddress.getByName("www.ibiblio.org");
      InetAddress helios = InetAddress.getByName("helios.ibiblio.org");
      if (ibiblio.equals(helios)) {
        System.out.println("www.ibiblio.org is the same as helios.ibiblio.org");
      } else {
        System.out.println ("www.ibiblio.org is not the same as helios.ibiblio.org");
      }
    } catch (UnknownHostException ex) {
      System.out.println("Host lookup failed.");
    }
  }
}
baidu.com is not the www.baidu.com

 

  二、URL

  三、HTTP

posted @ 2018-05-09 15:06  BigJunOba  阅读(322)  评论(0编辑  收藏  举报