Application of network-related APIs in Java InetAddress class
The InetAddress class is used to identify hardware resources on the network, representing Internet Protocol (IP) addresses .
The InetAddress class does not have a constructor method , so it cannot directly new an object;
The InetAddress class can obtain inetaddress objects by means of the static method of the InetAddress class;
1 Inetaddress.getlocalhost (); //Get local object 2 inetaddress.getbyname (""); //Gets the specified Name object
Main methods used:
1 //get the InetAddress instance of this machine2InetAddress add=inetaddress.getlocalhost ();3SYSTEM.OUT.PRINTLN ("Local Computer name:" +add.gethostname ());4SYSTEM.OUT.PRINTLN ("Local IP address:" +add.gethostaddress ());5 byte[] bytes=add.getaddress ();//gets the IP address in the form of a byte array6SYSTEM.OUT.PRINTLN ("IP address in byte array:" +arrays.tostring (bytes));7 System.out.println (add);8 9 //get inetaddress instances by machine nameTenInetAddress add2=inetaddress.getbyname ("laptop-cu3il302"); OneSYSTEM.OUT.PRINTLN ("Local Computer name:" +add2.gethostname ()); ASYSTEM.OUT.PRINTLN ("Local IP address:" +add2.gethostaddress ()); - //get InetAddress instances based on IP address and machine name -InetAddress add3=inetaddress.getbyaddress ("laptop-cu3il302", bytes); theSYSTEM.OUT.PRINTLN ("Local Computer name:" +add3.gethostname ()); -SYSTEM.OUT.PRINTLN ("Local IP Address:" +add3.gethostaddress ());
Operation Result:
Conclusion:
GetHostName ()----> Get machine name
- gethostaddress ()-----> Get IP address
GetAddress ()----> Get An array of IP address bytes , corresponding to IP address one by one
SYSTEM.OUT.PRINTLN (ADD)-----> Direct Output Object information is the computer name/IP address
Second, URL Uniform Resource Locator
1) Concept
- URL: A Uniform Resource locator that represents a network resource on the Internet.
- The URL consists of two parts: the protocol name and the resource name , separated by colons .
For example: https://www.cnblogs.com/is the homepage URL of the blog Park, where HTTP represents the protocol (Hypertext Transfer Protocol), and the following URL is the resource name.
2) URL Common methods
A URL class is provided in java.net to represent the URL.
URL common methods exist in the java.net package, providing methods such as creating url/sub-URLs, obtaining URLs, and so on.
---------------use the basic steps of the URL-----------------
First step: Create a URL object
Here are two ways to create a URL object, one that directly creates a URL for the specified address, and a new URL that is nested on the URL object you just created.
1 // create an instance of a URL 2 URL blog =new URL ("https://www.cnblogs.com"); 3 // ? The following represents the parameter, #后面表示锚点 4 URL url=new url (blog, "/index.html?username=hysum#test");
Note: Here the description of the URL parameter is not explained in detail, then will write a special article on this aspect (still learning Kazakhstan).
Step two: Invoke the relevant information using the URL method
1System.out.println ("protocol:" +Url.getprotocol ());2System.out.println ("Host:" +url.gethost ());3 //If no port number is specified, the default port number is used, at which time the Getport method returns a value of-14System.out.println ("Port:" +Url.getport ());5System.out.println ("File path:" +Url.getpath ());6System.out.println ("File name:" +url.getfile ());7SYSTEM.OUT.PRINTLN ("Relative path:" +url.getref ());8System.out.println ("Query string:" +url.getquery ());
Operation Result:
FAQ: Why is the Getport method returning-1?
A : if no port number is specified, the default port number is used, at which point the Getport method returns a value of-1.
FAQ: What is the file path/filename/relative path/query string represented in the URL?
A : file path-that is, the path without any special symbols;
FileName--File path +? The following parameters;
Relative path--#后面的锚点;
Query string--? The following parameters.
Step three: Read Web content
1. The OpenStream () method of the URL object allows the input stream of the specified resource to be obtained.
2. The data on the network can be read and accessed through the input stream.
The basic implementation steps are as follows:
- Generate URL Object
- The byte input stream is obtained through the OpenStream method, i.e. InputStream
- Wrap the InputStream into a character input stream InputStreamReader ( constructed according to the encoding format of the Web page )
- Packed into buffered streams for increased efficiency
- Declares a string to store the read content through the while loop output (not unique)
- Close all the streams, close
1URL url=NewURL ("http://www.baidu.com");2 //get byte input stream through OpenStream method3InputStream is=Url.openstream ();4 //Convert into character input stream5InputStreamReader isr=NewInputStreamReader (IS, "Utf-8");6 //increased read efficiency with buffering7BufferedReader br=NewBufferedReader (ISR);8 String date;9 while((Date=br.readline ())! =NULL){Ten System.out.println (date); One}
Attention:
Fourth step: Save Web content as HTML file
1 bufferedreader br=new BufferedReader (ISR); 2 PrintWriter pw=New printwriter ("c:\\users\\acer\\desktop\\baidu.html"); 3 String date; 4 while ((Date=br.readline ()) =null) {5 pw.println (date); 6 Pw.flush (); 7 }
After running, my desktop generates a baidu.html file:
Double-click Open, found to be garbled, this is actually the browser encoding parsing problem.
To change the browser encoding to Simplified Chinese, it can be displayed correctly.
Application of network-related APIs in "socket Programming" Java