Java Network Programming from entry to mastery (23): HTTP message header field

Source: Internet
Author: User



I. Common header fields

1.Connection

This field is only available inHTTP1.1Protocol. It determines whether the server immediately closes the network connection after a session is held between the client and the server. The most direct manifestation on the client is the useReadMethod(ReadLineThe same is true.)After reading the request from the clientWebWhether to return immediately after the resource-1(ReadLineReturnNull).ConnectionThere are two values:CloseAndKeep-Alive. When usingConnection:CloseTime, andHTTP1.0The Protocol is the same, whenReadMethod to return immediately when reading data.Connection:Keep-Alive,ReadThe method is blocked for a period of time after reading data. After the time-out period for direct data reading. As discussed in the previous articleReadHttpResponse (...)The 011st rows implemented by the method can verify the function of Connection. Next, let's use the HTTP Simulator for an experiment.

(1) enter the following domain name in the HTTP simulator:

Www.baidu.com

(2) HTTPInput the followingHTTPRequest Information:

GET / HTTP / 1.1
Host: www.baidu.com

(3)Press ENTER twice (enter an empty line), send the request message, and get 1As shown inHTTPResponse Message Header:

Figure 1

(4)InputYOrY(Enter Y or y immediately after the http response header is displayed) to display the content of the Response Message. After the content is displayed10Seconds before entering"Host: port>"Prompt (becauseSendHttpRequest ()(Row 004 in the Implementation Code sets the data read timeout ).

(5) Press enter at the "host: port>" prompt and enter the last domain name www.baidu.com and port 80. Enter the following HTTP request again:

GET / HTTP / 1.1
Host: www.baidu.com
Connection: close

After entering the preceding HTTP request, re-Execute steps 3rd and 4. After the HTTP Response Message is displayed, the "host: port>" prompt is directly entered. In addition to this method, change the first line of the request to GET/HTTP/1.0. In this way, you do not need to wait for the end.

By setting Connection, you can immediately disconnect the network after downloading Web resources (such as multi-thread download tools and Web browsers), which can effectively reduce the client resource consumption.

2.Date

ThisDateThe header field describes the time when the request message and Response Message were created. This field value isHTTP-dateType. Its format must beGMT(Greenwich) Time,GMTThe time is Beijing time minus8Hours. Below isDateAn example of a field:

Date: Tue, 15 Nov 2007   08 : 12 : 31 GMT

3.Content-Length

Specifies the number of bytes of the message entity. In the request messagePOSTThe method must be used.Content-LengthTo specify the number of bytes of the request message. In the response message, this field value specifies the currentHTTPResponse returnedWebThe number of bytes of the resource.

II,HTTPRequest message header field

1.Host

HostField

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.