Java Network Programming From Novice to Professional: HTTP header field



First, the common header field

1. Connection

This field exists only in the HTTP1.1 agreement.It determines the client and server for a session, the server is immediately shut down the network connection.In the performance of the client is to use the most direct read method (readLine method is the same) client requests the Web reading resources, whether immediately return -1 (readLine returns null).There are two values of Connection: Close and Keep-Alive. When using the Connection: Close when and HTTP1.0 agreement is the same, when read data read method returns immediately; and use the Connection: Keep-Alive time, read methods in readingOnce the data have to be blocked for some time.Read the data after the timeout, but continued down the execution.In the previous article discussed readHttpResponse (... ...) method implementation of the first 011 lines to verify the role of Connection.Let's do an experiment using the HTTP simulator.

(1) input the following HTTP simulator domain name:

www.baidu.com

(2) HTTP Simulator HTTP request to enter the following information:

GET / HTTP/1.1

Host: www.baidu.com

(3) double carriage return (enter a blank line), send the request message, and as shown in Figure 1, the HTTP response message headers:






Figure 1

(4) after input y or Y (in the display http response header, immediately enter Y or y), displays the contents of the response message.After the display contents, about 10 seconds to enter the "host: port>" prompt (as in sendHttpRequest () implementation of the 004 line of code to read data timeout set).

(5) in "host: port>" prompt, press enter, enter the last name used www.baidu.com and 80 ports.Re-enter the following HTTP request:

GET / HTTP/1.1

Host: www.baidu.com

Connection: close

After entering the above HTTP request, the re-run the first step 3,4.Finally, the contents of the display after the HTTP response message, directly straight into the "host: port>" prompt.In addition to this method, read the first line of the request GET / HTTP/1.0. This can not wait, exit.

By setting the Connection, you can download Web resources (such as multi-threaded downloading tool, Web browser, etc.), immediately disconnect from the network, which can effectively reduce the client's resource consumption.

2. Date

The Date header field describes the request messages and response messages are created.This field value is an HTTP-date type, it must be in the GMT (Greenwich) time, GMT time is GMT minus 8 hours.Here is an example of the field Date:

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

3. Content-Length

The number of bytes specified message entity.POST method in the request message must use the Content-Length request message to the specified number of bytes in the entity content.In response to the message field values specified in the HTTP response returned by the current Web resources, the number of bytes.

Second, HTTP request header field

1. Host

Host field is used to specify the resources accessed by the client host name and port number.If the port number to use when connecting to the server is equal to the port number, the port number can be omitted.Here is an example using the Host field:

Host: www.sina.com.cn

This field is necessary, if the HTTP request does not contain this field, the server will return 400 (Bad Request) response status.

2. Accept

Accept header field to determine the client can receive the media type.General format is "*/*" or "type /" subtype. "Sub-paragraph of this head can pass multiple media types, separated by", "separated.Accept if the following is an example:

Accept:: image / gif, image / jpg

If the Accept request header field value using the above, the server-side dynamically generated pages in the IMG will be the first head when the image contains the gif format, if the gif image does not exist, contains jpg format images.

3. User-Agent

This field is first used to specify what the client access to the server, if it is IE6 browser, and the unit is installed.net 2.0, the User-Agent will have the following values:

User-Agent: Mozilla/4.0 (

compatible;

MSIE 6.0;

Windows NT 5.2;

SV1; Maxthon;

. NET CLR 1.1.4322;

. NET CLR 2.0.50727;

InfoPath.1; InfoPath.2)

Check the server can use this field version of the browser client, and to determine according to the different versions of the data sent to the client.

4. Range

Range field is transmitted only through the server part of the first Web resources.This field can be used to implement HTTP header function.There are many download tools is through this field in the HTTP header.Range fields can be set in three formats range of bytes to be transmitted:

(1) Range: bytes = 1000-2000

Transmission range from 1000 to 2000 bytes.

(2) Range: bytes = 1000 -

Transport Web resources after the first 1000 bytes of all content.

(3) Range bytes = 1000

Transfer the last 1000 bytes.

Third, HTTP response message header field

1. Accept-Ranges

This field shows whether the Web server supports Range (whether to support the HTTP functionality), if supported, the return Accept-Ranges: bytes, if not support, the return Accept-Ranges: none.

2. Content-Range

Back to the Web resource specified byte range.The format of the value of this field is:

Start byte position - end of the byte position / Web resources, the total number of bytes

Here is an example of using the Content-Range:

Content-Range :1000-3000 / 5000

Test

Connect to the server in the HTTP simulator files.cnblogs.com, and enter the following HTTP request message:

GET / nokiaguy / HttpSimulator.rar HTTP/1.1

Host: files.cnblogs.com

Range: bytes = 1000 -

Returned response header shown in Figure 2:






Figure 2

It can be seen from the server following the transfer function files.cnblogs.com support breakpoint.But also to verify the Content-Length value is the current number of bytes in the session pass over, not the Web resources, the total number of bytes.The Content-Range field values in the "/" followed by the number of Web resources is the total number of bytes.

3. Location

This field specifies the address on which the new access to new Web resources.When the Web resource replacement URL, and users can not catch up on some of the old the new URL; Therefore, in the old users to access the original URL, use the Location to the new URL back to the client.This address translation is completely transparent to the user.