20150108--http protocol +php Analog HTTP request-02

Source: Internet
Author: User

HTTP protocol

1. What is the HTTP protocol?

Protocol: A protocol is a standard agreed by both parties, the two sides in doing something, must follow the predefined norms to achieve and operate.

HTTP: Hypertext Transfer Protocol, hypertext Transport Protocol, hypertext is the protocol that must be followed when transmitting between the server and the browser.

2. What is HTML?

Hypertext Markup Language: Hypertext Markup Language

The role of HTML: to ensure that the same data on different computers (users), see the same effect. How the HTTP protocol works

HTTP protocol Features

Support client/Server mode: HTTP protocol is generated for b/s, is based on the Web, the HTTP protocol is not limited to B/s, anything that can make HTTP requests, can use the HTTP protocol to access the server. Telnet Client

Simple and fast [just request method and path]: When a user initiates a request through a browser: Enter the URL

Flexible [arbitrary data]: can transfer any data type, whether it is a character stream or a binary stream

No connection [only one request is processed per connection]: When the browser initiates a request, the server can only respond once to the current request, and the connection closes when the response is complete.

Stateless [Transactional no memory]: the same browser repeatedly requests to the server, the server does not know the content of the HTTP protocol

The HTTP protocol is divided into two types depending on the participant's participation: HTTP request and HTTP response (Response) HTTP request

The HTTP request is divided into 4 parts: request line, request header, blank line, request data, four parts are independent rows (\ r \ n)

Request Line: Request file path (URI) protocol version

Request Method: Get/post

The URI of the request file: The section after the domain name. /gz/index.php

Protocol version: http/1.1

Request Header: The request header is a collection of specific protocols, one row for each individual protocol, and two sections for each line: protocol name: The content of the Protocol

Common Request Headers

Host: hostname (domain name), necessary

Accept-encoding: What type of data is allowed to be transferred by the server, gzip

Referer: Reference, the current request is initiated again after the last request

Connection: Connection status, how the connection should be handled after the request has ended keep-alive

Accept-language: The language to be received, Cn,en

Cookies: Carry data stored on the browser side

User-agent: Kernel information for current browser

Accept: Can receive the data, text,image ...

Content-length (POST): The length of the data submitted by the current browser,

If-modified-since (GET): The current browser has a local cache, asking the server if the requested script has been updated

Content-type (POST): Data type

Blank line: Used to end the request header (not a fixed number of request headers)

Request data: There will be request data when the POST request

Full HTTP request

HttpWatch can be used to grab packets: Crawl HTTP requests and responses, HttpWatch compatibility is not good

Telnet Impersonation HTTP request

Under Windows, a client Telnet provides remote access to the computer

1. Turn on the Telnet client

A) Open the Control Panel

b) Select "Programs and Features"

c) Click "Turn Windows features on or off" on the left.

d) Tick "Telnet Client" and click OK

2. Open the CMD console

3. Enter the telnet command to connect to the computer: Telnet host address port (telnet localhost 80)

4. Data echo: Press and hold CTRL + RIGHT Bracket

5. After the corresponding character appears, press ENTER

6. Start simulating HTTP sentiment: delete is possible, but does not empty the display

A) Request line: get/20150108/demo01_http.php http/1.1

b) Request header: HOST

c) Blank line: Direct carriage return

Note: When Telnet connects to the server, be sure to do it fast.

Recommendation: Write in the file first, then assign a value to paste the HTTP response

The rules that the server follows when it returns data to the browser.

The HTTP response consists of four parts: status line, response header, blank line, response body (message body)

Status line: Protocol version Status Code status description (http/1.1-OK)

Response header: Consistent with request header

The common response headers are as follows:

Server: Servers information

Date: datetime of the response

Last-modified: The last modification time of the currently requested file, with the if-modified-since in the request header

Content-length: Data Length (bytes)

Content-type: Data type, text/html,image/png

Location: Redirect, tell the browser, re-initiate the request

Refresh: Reset, redirect after specified time

Content-encodeing: Accept-encodeing in the corresponding request

Cache-control: Cache control that tells the browser to not cache data for the current request no-cached/cached

Blank line: Used to end the response header to differentiate between the response header and the data (response body)

Response Body: HTML code

Modifying the HTTP response

PHP can modify the HTTP response, but it is done through the header.

1. Setting the display encoding

2. Jump: Location

3. Refreshing: Refresh

4. Image output: The data given by the browser's default server is text/html

5. File Download: Modify the data transfer stream and modify how the browser receives the data. PHP impersonation HTTP request GET request impersonation

1. PHP tries to connect to the server

Fsockopen: Open the server, establish a connection, get a connection resource

2. Initiating an HTTP request: Four parts (get only three parts)

GET request: Request line, request header, and blank line

Make a request

Fwrite: Writes the corresponding data to a connection resource, returning the length of the data written

The data at the moment the request is made, the server has responded well, and the data is returned, and the data is saved in the resource.

3. Receiving data/output data

Extracting data from a resource

Looping through the entire resource

4. Close the connection

POST request

The Post request method basically is much worse, just must have several more request header: Content-type,content-length, request data

20150108--http protocol +php Analog HTTP request-02

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.