Basic knowledge of HTTP protocol

Importance of HTTP protocol:

Whether it is to use webservice in the future, or to use reset as a large-scale architecture, it is inseparable from the understanding of the HTTP protocol. It can be simplified to say:

webservice=HTTP protocol+XML

rest=HTTP protocol+json

Various APIs are also implemented with HTTP+XML/json

Ajax learning also requires HTTP protocol foundation

Required knowledge includes:

1. PHP+socket programming to send HTTP requests

2. PHP batch posting

3. HTTP protocol anti-leech

Optimization: HTTP Protocol and Cache Control

HTTP protocol and cookies

persistent connection


Format request for HTTP request information and response information:

1. Request line

2. Request header information

3. Request subject information

The request line is divided into 3 parts:

Request method Request path Protocol used

The request methods are: GET POST PUT DELETE TRACE OPTIONS

Execute the command in the window console: telnet 127.0.0.1 80 Then press the "ctrl+]" key, press Enter, enter the command in it, and you can see the command echo

 


Summary of HTTP request and response methods


Commonly used request methods are:

GET POST HEAD PUT TRACE DELETE OPTIONS

Note: Although these request methods are specified in the http protocol, the web server may not allow or support these methods

HEAD method: only return response header information


PUT method: upload a file or content to a website


TRACE method: For example, using a proxy to access a certain website, if you want to check whether the proxy has modified the HTTP request, you can use the trace to test it. The target website that is actually requested will return the last request received to the user.

OPTIONS method: Returns the request methods that the target server can support.


Status code and status text:

The status code is used to reflect the corresponding situation of the server.

The most common are 200 OK, 404 NOT FOUNT, 503 INTERNAL ERROR

The status text uses Ali to describe the status code, which is easy for people to view.

Status codes are divided into 5 series according to the corresponding status:


Common request success status codes

200 - The server successfully returned the web page

301/2 - permanent/temporary redirect. 307 redirect: The POST method is still used when the form data of the post is redirected.

Write a PHP program:

<?php
header('location: http://www.baidu.com');
//
header('location: http://www.baidu.com', true, 301); //Permanent redirect


 Accessing the PHP program in a browser will output the following: 
 

But if there are two script files on the server, such as a.php and b.php, a.php redirects to b.php, and a.php uses a form (POST) to submit data, then the data will be lost. Data submitted by the GET method will not be affected.

If you want the POST data of the previous page to be redirected to still be sent to the redirected page, you should use a 307 redirect.

304-Not Modified: When the browser requests a resource, if the server tells the browser that it has not been modified, then the browser uses the local cache instead of requesting the server, reducing the transmission pressure on the server and speeding up the response time on the browser side.

If the browser requests an image on the server, and if the browser caches the image, then when the request is refreshed again, the browser will send the content pointed by the arrow on the way to tell the server if the resource has been modified after that time. , the resource will be retransmitted; or if the ETAG in the second line does not match, the resource will also be retransmitted. The server does not know whether the client has a cache, but the browser's high-speed server.

Therefore, for website construction, for infrequently changed resources such as pictures, the cache time can be increased and repeated transmissions can be reduced.


Common Request Failure Status Codes

404 - The requested page does not exist

503 - The server is temporarily unavailable

500 - Internal Server Error









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324408488&siteId=291194637