---restore content starts---
http://www.blogjava.net/nokiaguy/archive/2009/02/archive/2009/05/archive/2009/05/archive/2009/05/archive/2009/ 05/archive/2009/05/archive/2009/nokiaguy/archive/2009/05/archive/2009/06/archive/2009/nokiaguy/archive/2009/06 /archive/2009/06/archive/2009/06/09/280837.html
Before we discuss the specific request and response header fields of the HTTP protocol, let's take advantage of the knowledge we've learned to implement an HTTP emulator. The so-called HTTP simulator can be used by the user to enter the HTTP request message, the emulator will send an HTTP request to the appropriate server, and then receive the server's response message. This HTTP simulator has several features:
1. You can enter the HTTP request manually and send it to the server.
2. Receive the response message from the server.
3. The message header and entity content are segmented, that is, it is not like a client such as Telnet will respond to HTTP
When the message is displayed, the message header is displayed first, and then the user decides whether to display the entity content.
4. Centrally send requests. Unlike Telnet, this HTTP emulator is not the first to connect to the server,
Instead, the server is connected and sent to the server after the domain name, port, and HTTP request messages are all lost . Doing so can prevent the server from shutting down network connections prematurely.
5. you can cycle to do the above operation.
From the above description, the following five steps are required to implement this HTTP emulator:
1. Create a large loop in which the inside of the loop is a request/response pair. This makes it possible to send multiple requests/responses to the server. The following four steps are included inside the loop.
2. read the domain name and port from the console, this function can be by Readhostandport (...) To complete.
3. Read the HTTP request message from the console, this function is by readhttprequest (...) To complete.
4. Send an HTTP request message to the server, which is done by Sendhttprequest ().
5. Read the server loopback HTTP response message, this function by Readhttpresponse (...) To complete.
Let's step through these five steps:
First, establish a cycle
Before creating this loop, first create a class called Httpsimulator, and define a run method in this class to run the program. The implementation code is as follows:
---restore content ends---
Second, Readhostandport (...) implementation of the method
The main function of this method is to read the domain name and port from the console. The domain name and port are separated by ":", ":" and the domain name and the port cannot have spaces between them. When reading a "q" from the console, this function returns FALSE, indicating that the program can exit, otherwise returns true, indicating that the domain name and port entered are correct. The implementation code for this method is as follows:
Focus
Java network programming from getting started to mastering (22): Implementing an HTTP Emulator