The common Java API Network

Java programs can HTTP service on a very convenient access to the Internet, FTP services, etc., and can be made directly to remote resources on the Internet, you can also send GET, POST request to the remote resource.

A, InetAddress class

  This class represents an Internet Protocol (IP) address, which has two subclasses Inet4Address and Inet6Address, representing the IPV4 and IPV6. InetAddress class does not provide a common constructors, but provides several static methods to obtain InetAddress instance.

  Get InetAddress instance method:

static InetAddress getLocalHost (): Returns the local host. 
static InetAddress getByName (String host): determining the IP address of a host, the host name given 
static InetAddress getByAddress (byte [] addr ): In the case of a given original IP address, returns InetAddress object. 
static InetAddress getByAddress (String host, byte [] addr): create InetAddress based on the host name and IP address provided by   
static InetAddress [] getAllByName (String host ): In the case of a given host name, according to the configured name service on the system returns to its an array consisting of IP addresses 

   Common methods:

public String getHostAddress (): Returns the IP address string (textual presentation). 
public String getHostName (): Gets the host name for this IP address 
public String getCanonicalHostName (): Gets the fully qualified domain name for this IP address 
public boolean isReachable (int timeout): Test whether that address is reachable. 
public byte [] getAddress (): Returns this object's original IP address InetAddress 

   Demo:

 1 public class TestInetAddress {
 2     public static void main(String[] args) throws Exception{
 3         InetAddress ip1 = InetAddress.getLocalHost();
 4         System.out.println(ip1);//njf/192.168.233.1
 5         
 6         InetAddress ip2 = InetAddress.getByName("www.baidu.com");
 7         System.out.println(ip2);//www.baidu.com/182.61.200.6
 8         
 9         byte[] ip = {(byte)182,61,(byte)200,6};
10         IP3 = InetAddress of InetAddress.getByAddress (ip);
 . 11          // output ip domain instead. If the IP address of the DNS server does not exist or does not allow ip -> domain mapping, getHostName () method returns directly to the IP address 
12 is          System.out.println (IP3);
 13 is      }
 14 }

 

 

Two, URL

  URI (Uniform resource identifier): Represents a Uniform Resource Identifier (URI) reference, used to uniquely identify a resource.

  URL (Uniform Resource Locator): Class URL represents a Uniform Resource Locator, which is a pointer to a "resource" Internet. Resources can be a simple file or directory can also be a reference to more complex objects, such as a query to a database or search engine. It is a specific URI, namely URL can be used to identify a resource, but also indicates how locate this resource. URI can not be used to locate any resources, its sole function is to parse, and the URL contains an open resource reaches the input stream.

  The basic structure of the URL consists of 5 parts:

<Transfer Protocol>: // <host name>: <port number> / <filename> fragment # Name

<Transfer Protocol>: // <hostname>: <port number> / <filename> parameter list?

    Where # fragment name: the anchor

    For example: http: //java.sun.com/index.html#chapter1

    Parameter list format: parameter name = value & parameter parameter name = parameter value ....

    例如:http://192.168.1.100:8080/helloworld/index.jsp?username=chai&password=123

   URL构造方法:

 

   URL常用方法:

三、

四、

五、

Guess you like

Origin www.cnblogs.com/niujifei/p/12290288.html
Recommended