Java network programming from beginner to proficient (+): Format of HTTP messages

Source: Internet
Author: User
Tags error status code response code

When a user enters a URL based on an HTTP protocol in a browser (a URL that beginswith//), it is equivalent to a notifier that generates an HTTP request at the URL and sends a request to the server, while waiting for the server to respond. Both the request and the response are collectively referred to as HTTP messages.

The HTTP protocol is divided into two parts:

1. HTTP request message.

2. HTTP response message.

First, HTTP Request Message

The HTTP request message is divided into two parts: the request message header and the request entity content. The first line of the request message header must consist of the following three parts:

The requested method (GET, post, head, and so on).

The path to the Web resource (the/test/test.html section in http://www.website.com/test/test.html).

Version of the HTTP protocol (http/1.0 or http/1.1).

The other line in the request message header is the Request header field. The format of each row is:

"Header Field": "Header field value"

The request header ends with a blank line. As an example of the previous GET request :

get/http/1.1 Host:www.csdn.net connection:keep-alive Accept: */*

In the case of a POST request, the entity content to be submitted is placed after the empty line of the message header, as shown below is an example of a POST request :

Post/servlets-examples/servlet/requestparamexample http/1.1 host:localhost:8888 content-length:29 connection:close
Firstname=bill&lastname=gates

In the request message above, Content-length represents the length of the requested content in bytes (the length of "firstname=bill&lastname=gates"). When using the Post method, this field must be provided, and the length must be equal to the length of the entity content, or the server will return an error status code.

Second, HTTP Response Message

The HTTP response message is also divided into two parts: message header and entity content. The HTTP response message header is similar to the request message header, and the first line is the result of the request, that is, the first line in the response message header indicates whether the request message succeeded in obtaining the Web resource on the server. The first line must consist of the following three parts:

1. the HTTP version of the response message.

The format is http/1.1 or http/1.0. This version number is not necessarily the same as the version number of the request message header;

Because the server does not necessarily support HTTP versions described in HTTP requests, such as using get/http/1.1 to request a server, when the server only supports HTTP1.0, it returns http/1.0.

2. status Code.

This status code consists of three digits, divided into five grades. Here is the response code for HTTP1.1:

(1) A number starting with 1 (1xx).

Temporary request status code. Made up of 100 and 101. Such status codes are not often used, as they are primarily the temporary state returned by the server for the client.

(2) A number starting with 2 (2xx).

Request Success Status code. range from 200 to 206. The most common of these is 200, which indicates that the client request was successful and the server has returned the requested Web resource to the client. The other six status codes are similar, indicating that the request was successful, just to guide the client in and the next step. If the status code 206 indicates that the server just returned a portion of the request resource, the client must continue to make an HTTP request to obtain all of the Web resources. The meaning of the other response codes can be found in the canonical RFC2616 of the HTTP1.1.

(3) A number starting with 3 (3xx).

Web resource redirection status code. range from 300 to 307. All status codes that begin with 3 change the original URL for different reasons and ways for the web resource. As 302, a location field determines where the Web resource changes after the URL. With this status code, when the URL of a Web site or other Web resource changes, the user accessing those Web resources does not even know the changed URL, so you can add a 302 response when the user accesses the original URL, allowing the client to automatically access the new URL.

(4) A number starting with 4 (4xx).

Client Error status code. range from 400 to 417. Maybe no one wants to see this status code. But they do exist massively on the Internet. When a user accesses a Web resource that does not exist or does not have permission to access the Web resource, the server returns such a status code. Most commonly encountered in this type of status code is 400. When a user sends a non-existent web resource path (the path followed by the GET, post, and head methods), the server returns the status code.

(5) A number starting with 5 (5xx).

Server error status code. range from 500 to 505. This kind of status code is also a kind of error status code, but it is different from 4xx, 5xx error is caused by the server, such as the user to the server to send an HTTP protocol does not support the method, such as GET1, the server will return 501 error, indicating that the server does not support this HTTP request method.

3. status information.

The content and status codes of the response information are closely related. If the status code is 200, the status information is "OK". The status code is 501, and the status information is "not implemented". To learn more about the status information for each status code, refer to the HTTP1.1 specification RFC2616.

The rest of the HTTP response message, like the request message, consists of a number of response header fields, one row per field and one field value. The response message header uses a blank line to end, followed by an empty line that follows the entity contents of the HTTP response message. The following is an example of a complete HTTP request and response:

HTTP Request Message

get/http/1.1 Host:www.csdn.net

HTTP Response Message

http/1.0 OK content-length:132273 content-type:text/html content-location:http://www.csdn.net/index.htm Last-modified:sun, Jan 09:20:00 GMT accept-ranges:bytes ETag: "eed72b7cbd42c71:1b0e" server:microsoft-iis/6.0 X -powered-by:asp.net Date:sun, Jan 09:23:26 GMT age:32 x-cache:hit from Cachebj244.csdn.net Connection:close & lt;! DOCTYPE html

As can be seen from the HTTP message above, the HTTP version in the request and response messages is not the same, which means that the CSDN Web server is compatible with only HTTP1.0-only clients and uses the HTTP1.0 protocol.
Note: The request method in the first line of the HTTP request and Response message (GET, post, and so on) and the letters in http/1.1 or HTTP1.0 must be uppercase, and the path case after the request method is OK. The message header fields are not case sensitive, but most of the request and response field names are capitalized in the first letter.

Java network programming from beginner to proficient (+): Format of HTTP messages

Related Article

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.