HTTP Learning Note 1

Source: Internet
Author: User
Tags ming file transfer protocol ftp protocol

Conditions of communication preschool small story

Through this story, we understand the communication between the two computers, what must be the conditions?

One day, this Xiao Ming and Xiao Qiang, a cattle in the mountain, a grass on the head of the mountain. However, because of boredom, this Xiao Ming is like looking for the opposite of the cockroach chat. Because this distance is far, can not see clearly, this little cockroach is in the opposite do! It shouted: "Xiao Qiang, are you mowing the grass?" The cockroach heard someone calling him. Raised his head, vaguely see someone in the opposite of the hill, but the mountain high wind, the cockroach can not hear clearly what Xiao Ming asked him. (He thought Xiaoming was asking him something else) and he went back: No, I cut the grass here. The Xiao Ming heard the cockroach respond to him, but he did not hear clearly. He just heard the little Johnny and said something about the grass. Then, this Xiao Ming thought he was not mowing the grass, he responded again: I thought you were cutting grass over there. In this way, the two people chatted all afternoon and chatted very happily. But, in the end, do not understand what the other party is doing.

These two people chat so happily, what kind of condition should it have?

1) First, the 1th, they can chat, two people must be able to see each other! So the first condition that must be available is within the visible range. (two people must see each other)

2) 2nd, these two people can chat, definitely speak the same language it!

3) 3rd, two people must know who they are! If these two people don't know each other, do you think they'll have a good time in the afternoon?

4) 4th, Xiao Ming shouted to Jack, this is through the sound to spread, he must have a medium bar! Therefore, it is necessary to have the media to disseminate information.

What are the conditions for communication between two computers?

I first drew two computers and listed the basic conditions that communication between the two computers must have.


1. Media to disseminate information

To analyze and discuss, first of all, the 1th computer to disseminate information, it must have the media to disseminate information. When we are transmitting information, it is transmitted through sound, which is our medium. When our computer is transmitting information, it is transmitted through our network cable. We draw two networks:


2. Environment of communication

With the network cable, not enough AH! The computer must have a communication environment if it communicates. Just like we were just this xiaoming and Xiao Qiang. The two of them have to talk, they have to be within the visible range to chat. Our computer is the same, it must have the same communication environment if it wants to communicate. Computers can communicate over the network:


3.IP

With this network and cable, the two computers are supposed to be able to communicate. But there is one more question. There may be thousands of computers in our network. So, so many computers, how can I find the opposite computer? (then it can't be found, in order to solve this problem!) We can name different computers in the same network. (This name, unlike the human name, is named by an IP address in the form of a letter or Chinese character.) And this IP address! It is actually a number of 4 groups of 0 to 255. To label the identities of different computers within the same network. We are now on the two computers to mark the IP address, ah! Within the same network, this IP address cannot be duplicated. (The communication between the a computer and the B computer is basically the same.)


4. Port number

So, do we have a computer that can communicate? (It is really able to communicate) but we know that our computer communication is actually through a network application to achieve communication. (for example, our QQ, we want to send information, through the QQ to send information) we must install some Web applications on each computer.


Then, this problem again, we send information through QQ, sent to the opposite computer, can find the computer on the opposite. But this opposite computer how does it know that I want to use QQ to process information? (This we have to consider) in fact, it is not known, then in order to solve the problem. When we install the Web application on each computer. For each network application, it must have a unique identity. With this unique identifier, we can find the corresponding Web application. (and, this unique identifier we call it: Port number (port) and this port number, it must be a number between 0 and 655. So, this QQ in the start, regardless of which computer, it has a unique identification number.

5.TCP/IP protocol

Now both computers have a basic condition for network transmission. Now I want to implement such a function, through the QQ app on Computer A, send a data "hello" to the QQ application on Computer B. A the computer must be processed to send the data. (Because, we know that the computer does not recognize the human language, so that this information must be processed to send) so, this process, we call it data packaging. This data packaging, in fact, is a bit like our usual life in the mail such a process. We know that we want to send something, send it somewhere, we first have to go to the post office to pick up the information sheet.
Fill in each other's receiving place. (Fill in the other person's consignee and so on a series of information) after filling out, the object wrapped well. The postman will then be able to pass the information on the information sheet to your friend's exact message. And our computer has to pass this well-designed data to the other computer, it is going through this process. (This is the process of data packaging) data packaging process, it is not to say that the packaging, the transmission of data, but also to follow a standard to package. What is the standard? is the basic protocol of network data transmission: TCP/IP protocol. With this protocol, we can make our data into a packet. Spread over the web to the opposite browser. Of course, who will act as the postman when it is transmitted over the Internet? is our router. Since you have this packet wrapped, the router will be able to identify the corresponding IP address of the packet. (and the port number of the other application). He will be able to accurately pass this packet to the other side of the computer.

So, when we pack this data, we have to follow this TCP/IP protocol.

What is the TCP/IP protocol

TCP/IP protocol, which is actually a set of protocols

By layer can be divided into 4 layers

First Layer: Application layer


In the application layer, it contains a series of protocols, such as HTTP Hypertext Transfer Protocol (which is also a protocol for our web application transmission), the second is the FTP protocol (File Transfer Protocol), and the other is SMTP (this is our Simple Mail Transfer Protocol). Of course, there are many others.

Second Layer: Transport layer


There are two protocols in the Transport layer: the TCP and UDP protocols.

Layer Three: Network layer


The network layer inside, it has a lot, I'm here to give two examples. IP protocol and ICMP protocol.

Layer Fourth: Network access Layer


Since this layer is related to our computer hardware, we don't discuss it in depth here.

TCP protocol

The full name of this TCP protocol is called: Transmission Control Protocol (transmission protocol)

The characteristics of this Agreement

It is characterized by a connection-oriented protocol, this connection-oriented to ensure that the data is accurate, there is no data loss of a situation.

How it's guaranteed.

First, this a computer sends a piece of data to B computer, ask this B computer is not in. If this B computer can receive this data, then B computer will return a word to a computer (send a data come), tell a computer: I am, you can send data come over. And this a computer after receiving the data of B computer, it is again send data past, tell B computer: I know you are in, now we can send data. Then, after these three data, it can ensure that the data is accurate to the B computer. These three times the data is actually what we usually say: three times handshake. This three-time handshake ensures that no packet loss occurs.

UDP protocol


Its full name is called: User Datagram Protocol.
Its English name is called: User Data Protocol.

Features of the UDP protocol

It is more TCP just the opposite, it is a protocol oriented to no connection. It doesn't matter whether it's on the other side or not, it sends out the packet. If the opposite computer is not there, it will throw away the packet.

Where will the two protocols be applied to, respectively?

For example, we download the file. This file download, if you use the UDP protocol, if the data is lost, your data download is not complete, this file is not open on the computer. Therefore, in order to ensure that our files can be downloaded accurately, we will certainly adopt the TCP protocol. And when we send QQ information, you send a data in the past, the opposite in the end have received, this is not really important. (if it's not received, you can do it again, so we'll use UDP.)

This agreement, what difference does it have


TCP connection-oriented, can ensure that the data can be accurately transmitted to the other computer, (but its transmission efficiency relative to UDP, it will be lower, because it has to go through a few handshake)
And this UDP, although the transmission efficiency is high, but it may occur data loss packets. Therefore, each has its own characteristics, we can not say which agreement is good, which is bad.

HTTP protocol

HTTP protocol

Its full name is called Hypertext Transfer Protocol.
Its English is called: Hyper Text Transfer Protocol.
This Protocol is a protocol established prior to TCP and UDP. (It is the protocol of the Application layer)

Why HTTP protocol is required


We know that TCP and UDP exist only to send data. And this data on the other side of the computer, how to explain this data? Well, we still have to follow the standard to resolve. For example, the website of our Wheat Academy, where we enter user information



Then, we click Submit login, then it sends a lot of data. (such as the kernel that will send us to use the browser, what browser, browser-supported compression, browser-supported language, as well as the user name and password I just submitted) then, this information will be submitted to the server, this information, if submitted to the server, How does the server parse this information?
We have to follow a standard to resolve. How your side is encapsulated into a packet, I will follow in what way to parse.
Like what:
The first line is the browser information;
The second line is the user's information;
The third line is some other information.
In this way, we can parse the data correctly. So, the HTTP protocol as an upload protocol, its existence must have its truth

What kind of protocol is the HTTP protocol

HTTP is the Hyper Text Transfer Protocol (an abbreviation for Hypertext Transfer Protocol), which is used to transfer hyperlinks from the WWW server to the local browser's application-layer transport protocol. That is, for example, I visit the Wheat Academy's homepage through a browser: Then, I return, the content appears, this is based on the HTTP protocol


It is usually hosted on the TCP protocol (the bottom of the HTTP protocol is TCP), and the Protocol is a stateless protocol based on the request/response pattern. The Web Association (World Wide Web Consortium) and the Internet Working Group Ieth (Internet Engineering Task Force) defined version 1.0 in 1990, currently the latest version 1.1.

HTTP Conceptual Analysis

The previous section of HTTP inside the information is more, let us analyze it one by one.

Request/Response Mode

What happened in the background when we entered the www.maiziedu.com string in the browser just now? Let's analyze it for a moment. (On the left is our Google browser, the right is the wheat Academy on the Internet host)


When I enter this string in Google Chrome, then, a hit enter. So what does my browser do?


It will first find the host of the Wheat Academy on the Internet and then establish a connection with it.


Once the connection is established, it will send a packet right away:


The process of sending the packet, we call the client's request, we use the English word request to name it.


Then, after the server receives this data, it makes a series of processing and finally responds to the client with the result of processing: (This process is called response (response))


Then, disconnect from our client:


It's about 4 steps.

As long as we have sent the data, it must have a request and a response. And after this request and response, it establishes a connection and then disconnects.

No state how to understand

Stateless actually means that we are the host of this wheat academy, it is actually not know a state of the client, do not know who is coming to visit me. I do not know how many times this client has been visited. Also do not know who established this connection. So, this stateless is the server does not know the state of the client. (This connection, after it is connected, gets the data, it immediately disconnects) (this server it only knows someone to access, but does not know who to access). This is the connection based on a stateless state.

What is the difference between versions 1.0 and 1.1


The 1.0 version, that is, every request, it establishes a connection, and then disconnects. (as soon as the request is finished, the response is finished, it disconnects) well, let's think about it: the Wheat Academy homepage so much data.


There are pictures, CSS style, have JS code, so a Web page, it contains a lot of things, it can not be in a request to all the data contained in the entire page. So, these files, we all need a separate request. Let's say we open the console and refresh the page again: the only one we're asking for is: www.maiziedu.com However, there have been many requests behind it. This time, if you use version 1.0. We request this homepage, it establishes a connection, requests this data, disconnects. So, let's ask the following, like this CSS:

Then it has to establish a connection, send the request, respond to the data, disconnect the connection. (and going through such a step) so many things, you have to build n multiple connections in a single visit, break n multiple connections, will certainly reduce access efficiency. This problem was solved in version 1.1. We specify a disconnect, say, 5 seconds.


In this 5 seconds, you set up a connection, as long as you have a new request to come over, I continue to open the connection, directly respond to the past. For more than 5 seconds, I'll disconnect you here. The next time you ask, you'll have to reestablish the connection. (This improves access efficiency)

Because, when we first visit the home of the Wheat Academy, it establishes the connection, in fact, we will immediately request a lot of data, send many requests. Note that the speed of this send request is very fast. It will definitely be able to complete so many requests within 5 seconds. When we get the last file, we never ask for the past, this time, more than 5 seconds, at this time of 5 seconds, there is a new request, it will disconnect the connection.

What do you mean URL

What do you mean URL


Just now we entered a string in the address bar, this string of strings is not a random loss. is in a certain format. This string of strings we call it URL: Uniform Resource Locator. Also say that all the resources on our network, we have to find it, we must give it a unique path. This unique path, we call it: Uniform Resource Locator.

Format of the string


Protocol://Host domain name or IP address: port/resource Path



1. Agreement

This protocol can be a series of http,https,ftp and so on. As long as it is followed by a colon, double slash, which is a fixed format. Behind this host domain name or IP address. Then there is the port number, and the path to the resource. Such a string can be composed of our URL, composed of our Uniform Resource Locator. (You can find the only resource)


Like http://www.maiziedu.com.
This protocol is HTTP. Our host domain name is www.maiziedu.com. The resource we requested is its homepage, so there is no input to find it.


2. Domain Name


A domain is the name of a host on our internet. In our browser to send a string of this URL, when we hit enter, it will be on the Internet to find the corresponding IP address of the domain name. Who is the IP address for this domain name to tell it? is implemented by something called a domain name resolver, called DNS.


This thing, first send the domain name to the DNS here, ask it this www.maiziedu.com this domain name IP address is how much. Then the domain name resolution system will send this request to the next time. (It may have recorded the current IP of the Wheat Academy) and it will look for the IP address that corresponds to the current one. The difference between the input IP address and the input domain name is that the IP address is not the aspect of our human memory, and our domain name is very convenient for our human memory. So, when we generally visit the site, we enter its domain name, not its IP address.

When this DNS resolves this domain name, its corresponding IP address will find the corresponding host on the Internet, which is the host of our Wheat Academy. This enables a request to be accessed. A connection was established.

3. Port number


4. Resource Path


This resource path must be a resource path on the server.
If the homepage of our Wheat Academy is:
Http://www.maiziedu.com/index.html this homepage. Then its resource path, the full path should be written like this. We will be able to access the corresponding homepage.

Request Packet

What is included in the request packet?


It consists of four parts

a) Request line;

b) the request head;

c) blank line;

D) the request body.

Request Line

How the request line in the packet gets

In this Firefox browser, I installed a plugin, this plugin is called: Httpfox. Click on it and the plugin will intercept each request packet we send data:


Here we refresh the Wheat Academy homepage, and then open this plugin. See it constantly sending requests, constantly parsing this packet.


I intercept this packet is our domain address, this URL of the packet.


Here it has a request header. The value of the following corresponds to it.


Inside the request header, the first line is its request line:


So, this is the thing that corresponds to us here:


That's what this is about.


The first is the way of the request.
Then spaces, slashes, spaces.
Then the version of the requested HTTP protocol is followed.

What the Request method contains

PUT: The request server stores a resource and uses Request-url as its identity.
Delete: The request server deletes the resource identified by the Request-url.
Post: Appends new data to the resource identified by Request-url.
Get: Request for the resource identified by the Request-url.
HEAD: Request a response message header for the resource identified by Request-url.
TRACE: Requests the server to echo received request information, primarily for testing or diagnostics.
CONNECT: Reserved for future use.
Options: Request performance of the query server, or query for resource-related options and requirements.

In so many requests, the most we use is the post and get properties. (In the actual development, the use of these two kinds)


The difference between the two


We didn't specify a GET request just now, so why is this a GET request?


Because we enter an address in the browser address bar, for example, we input www.maiziedu.com, I hit enter, here it is in fact the default is to send data by the way of get. And when will we use this post request? This is when we submit the form, it can use post (of course we need to specify) of course, you can also use get.

Get method, if the form is submitted, it will put the submitted data behind the URL. Will it be used? To separate the URLs and parameters, the parameters are separated from the parameters by the & symbol. If we submit the form, it will put the requested data into the URL to submit it.
But if we take this post request. It will put the data of this request into the part four we just said: inside the request body. (It is said that this time the user will not see)


Request Header

In this plugin of Firefox, it has a series of request headers:


And what does that mean? It also says that our request is sent to the server, which will bring some information about itself to the browser.

What kind of information does it take?


Host:www.maiziedu.com (that is, our corresponding host)
User-agent: Represents the operating system that we use, as well as the browser used and its kernel (for example: mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/51.0.2704.84 safari/537.36)
Accept: Some text types represented by our browser can handle (for example: image/webp,image/*,*/*;q=0.8)
Accept-language: Represents the natural language that our browser can handle. (Example: zh-cn;zh;q=0.8;en-us;q=0.5;en;q=0.3)
Accept-encoding: The code that represents it (for example: gzip,deflate)
Cookie: On behalf of the server we want to submit a string of data, of course, this cookie, we will explain later.
Connection:keep-alive (keep the connection, that is, not after I have requested, disconnect immediately)

Of course different types, it is not the same.

Blank Line


The purpose of a blank line is to separate the delimiter for our request header and message body. Just now we are talking about this request body, this browser wants the server to send the data after, store some data, it is placed in the request body inside.

Response Packets

The response packet is divided into 4 parts

These 4 sections are:

a) status line;

b) Response head;

c) blank line;

d) message body.

Status line


Needless to say, our Wheat Academy is based on the HTTP protocol version 1.1. And what is this 200? It is a status code. There's another OK in the back. This OK is the text description of the status code.


A status code is a state that is used to describe the current stage in which we are accessing this user. The status code is made up of three digits, the first number defines the category of the response, and there are 5 possible values: 1, 2, 3, 4, 5, respectively.

1XX: Indicates information-Indicates that the request has been accepted and continues processing.


2XX: Success-Indicates that the request has been successfully connected, understood, accepted.


3XX: Redirect-to complete the request, a further action must be made.


That is, if I visit this Wheat Academy server, this wheat Academy server determines that this resource cannot be found here. It can be found on another server. Then it sends a packet that tells you to request it on another server, so this process is called redirection, which we'll explain later.


4XX: Client Error-The request was not implemented by a syntax error or request.


For example, I write a random one in the back, and then enter. Well, it pops up a page here: (Of course, this is a page that the wheat Academy itself defines as an access error)


Let's take a look inside this browser: (Here's a 404 error)


What does it stand for? The address that represents the user output does not exist. The resource could not be found on the server.

5XX: Server-side error-the server failed to implement a legitimate request.


For example, the server side where the code is wrong, or where it fails, it will return the status code starting with 5 (we will also use code to demonstrate).

What are the common status codes?

200: On behalf of the data received successfully, processing, response.
302: Delegate redirection.
404: The representative of the resource does not exist, is what we have just seen the effect.
500: The server-side error is represented.

Response header

The response header is actually in correspondence with our request header. It is also preceded by a name, followed by a value.

What information is included in the response header

Date represents the time at which the response header is generated. (These response headers we write code can be defined)
Date sat,28 Nov 14:57:22 GMT

Server on behalf of this is the wheat college server is using Apache as a server, and is based on the Linux operating system (Ubuntu).
Server apache/2.4.7 (Ubuntu)

The code uses GZIP, which corresponds to our server.


Content-encoding:gzip
And what does Content–length represent? Represents the size of the data that is responding. 11114 bytes.
content-length:11114.

The time to set the timeout here is 5 seconds, and the maximum is no more than 100 seconds.
keep–alive:timeout=5;max=100;

The connection also keeps the long connection.
Connection:keep-alive.

Content-type represents a file type and a file encoding for this response.
Content-type:text/html;charset=utf-8;

Blank line and message body

Of course, a blank line is also used to split the response header and the message body. It also plays a role of separation. The message I put in this response to the data in the message body. Send it over to me.

Where do you see this?


For example here we open this plugin, find the content of this item, click to view a CSS file. Let's look below, this is the file sent to me in this response: (very much)


HTTP Learning Note 1

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.