Java Network Programming from entry to mastery (22): Implement HTTP Simulators

Source: Internet
Author: User

Before discussing the specific request and response header fields of the HTTP protocol, let's use the previous knowledge to implement an HTTP simulator. The so-called HTTP simulator can send an HTTP request to the corresponding server and then receive the Response Message from the server after the user inputs the HTTP request message. This HTTP simulator has the following features:

1. You can manually enter an HTTP request and send it to the server.

2. Receive the Response Message from the server.

3. The message header and entity content are displayed in segments. That is to say, the HTTP response is not sent to clients such as Telnet.

The message header is displayed first, and the user determines whether to display the entity content.

4. Send requests in a centralized manner. This HTTP simulator is different from Telnet in that it does not connect to the server from the very beginning,

Instead, connect to the server after all the domain name, port, and HTTP request messages are sent to the server. This can prevent the server from closing the network connection in advance.

5.You can perform the preceding operations cyclically.

From the above description, it takes the following five steps to implement this HTTP simulator:

1. Create a large loop, which contains a request/response pair. In this way, you can send multipleThe request/response takes effect. The following four steps are included in the loop.

2. Read the domain name and port from the console. This function can be completed by readHostAndPort.

3. Read the HTTP request message from the console. This function is completed by readHttpRequest.

4. Send an HTTP request message to the server. This function is completed by sendHttpRequest.

5. Read the HTTP Response Message sent back from the server. This function is completed by readHttpResponse.

Next we will gradually implement these five steps:

I, Create a large loop

Before creating this loop, create a class named HttpSimulator and define a run method in this class to run this program. The implementation code is as follows:

001 Package Http;
002
003 Import Java.net. * ;
004 Import Java. io. * ;
005
006 Public   Class HttpSimulator
007 {
008 Private Socket socket;
009 Private   Int Port =   80 ;
010 Private String host =   " Localhost " ;
011 Private String request =   "" ; // HTTP Request Message
012 Private   Boolean IsPost, isHead;
013
014 Public   Void Run () Throws Exception
015 {
016 BufferedReader =   New BufferedReader ( New InputStreamReader (
017 System. in ));
018

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.