Java Network Programming 4: parsing and usage examples of inetaddress, URL, and urlconnection

Source: Internet
Author: User

1. The inetaddress class implements simple IP scanning to obtain the Host Name

Package demo.net; import java.net. inetaddress; import java.net. unknownhostexception;/** get the Host Name of the specified IP Address */public class ipscanner {// input the IP address and return the host name, if the host cannot be reached, return the string form of the IP address public static string forward (byte [] IP) {inetaddress ADDR = NULL; try {ADDR = inetaddress. getbyaddress (IP);} catch (unknownhostexception e) {e. printstacktrace ();} return ADDR. gethostname ();} public static void main (string [] ARGs) {string hostna Me; string IP; For (INT I = 0; I <= 127; I ++) {IP = "59.68.255. "+ I; hostname = second (New byte [] {59, 68, (byte) 255, (byte) I}); If (! IP. Equals (hostname) system. Out. println (IP + ":" + hostname );}}}

 

2. url example

Package demo.net; import Java. io. ioexception; import java.net. URL; import Java. util. examples;/** URL class usage example. a url is a unified Resource Locator that can open a stream to a resource, to obtain the resource content * obtain the content on the homepage of Wuhan Institute of industry in the example. The returned content is HTML text */public class urldemo {public static void main (string [] ARGs) {try {URL url = new URL ("http://www.whpu.edu.cn"); entries in = new URLs (URL. openstream (); While (in. hasnextline () system. out. println (in. nextline ();} catch (ioexception e) {e. printstacktrace ();}}}

Running result: the source code of the HTML page is output.

 

3. urlconnection Class Example

Package demo.net; import Java. io. ioexception; import java.net. URL; import java.net. urlconnection; import Java. util. break;/** the urlconnection class is an abstract class. After locating the resource, you can read the resource content and obtain the header information *. It can also be used to submit form data, this example shows how to obtain resource and header information */public class urlconnectiondemo {public static void main (string [] ARGs) {try {// 1. Create a connection object by calling the openconnection method on the URL. URL url = new URL ("http://www.whpu.edu.cn"); urlconnection connection = URL. openconnection (); // 2. process set parameters and common request properties. Connection. setdoinput (true); connection. setdooutput (true); // 3. Use the connect method to establish the actual connection to the remote object. Connection. Connect (); // 4. The remote object becomes available. The header field and content of the remote object become accessible. System. out. println (connection. getcontenttype (); system. out. println (connection. getcontentencoding (); system. out. println (connection. getcontentlength (); system. out. println (connection. getdate (); // read the content in = new timeout (connection. getinputstream (); While (in. hasnextline () system. out. println (in. nextline ();} catch (ioexception e) {e. printstacktrace ();}}}

Running result: Some header information and HTML page source code are output.

4. Summary

Conclusion: A: The CONNECT () function of httpurlconnection actually only establishes a TCP connection with the server and does not actually send HTTP requests.
Whether it is post or get, the HTTP request is not officially sent until the getinputstream () function of httpurlconnection.
B: When using post to send a URL request, setting the order of URL request parameters is a top priority,
Configure everything for the connection object (that pile of set functions)
All must be completed before the connect () function is executed. The write operation on outputstream must be performed before the read operation on inputstream.
These sequences are actually determined by the format of the HTTP request.
If the read operation of inputstream is performed before the write operation of outputstream, an exception is thrown:
Java.net. protocolexception: cannot write output after reading input .......

C: The HTTP request is actually composed of two parts,
One is the HTTP header. All configurations for this HTTP request are defined in the HTTP header,
One is body content.
The CONNECT () function generates HTTP header information based on the configuration value of the httpurlconnection object. Therefore, before calling the connect function,
All configurations must be ready.
D: The HTTP header is followed by the HTTP Request body. The body content is written through the outputstream stream,
In fact, outputstream is not a network stream. At best, it is a string stream. What is written into it is not immediately sent to the network,
It exists in the memory buffer. When the outputstream stream is disabled, the HTTP body is generated based on the input content.
Now, all HTTP request items are ready. When the getinputstream () function is called, the prepared HTTP Request
It is officially sent to the server, and an input stream is returned to read the server's returned information for this HTTP request. Because HTTP
The request has been sent when getinputstream (including the HTTP header and body), so the getinputstream () function
Then set the connection object (modify the HTTP header information) or write outputstream (modify the body)
All of them are meaningless. executing these operations will lead to exceptions.

For more detailed urlconnection class parsing Content, see: http://www.blogjava.net/supercrsky/articles/247449.html

 

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.