Communication Protocols-HTTP, TCP, and UDP

Source: Internet
Author: User
Tags url example

 

Cp http udp:

All are communication protocols, that is, the rules observed during communication. Only when both parties "speak" according to this rule can the other party understand or serve it.

Relationship Between tcp http and UDP:

TCP/IP is a protocol group, which can be divided into four layers: network interface layer, network layer, transmission layer and application layer.
The IP protocol, ICMP protocol, ARP protocol, RARP protocol, and BOOTP protocol are available at the network layer.
There are TCP and UDP protocols in the transport layer.
The application layer includes FTP, HTTP, telnet, SMTP, DNS, and other protocols.
Therefore, HTTP is a transfer protocol that transfers hypertext from a Web server to a local browser.
Socket:
This is a communication pipeline established to achieve the above communication process. The actual representative is a communication process between the client and the server. Both processes communicate through the socket, communication Rules use the specified protocol. Socket is just a connection mode, not a protocol, TCP, UDP. Simply put (although not accurate) is the two most basic protocols. Many other protocols are based on these two protocols, for example, HTTP is based on TCP ,. you can use a socket to create a TCP connection or UDP connection. This means that you can use a socket to create a connection of any protocol, because other protocols are based on this.

Next we will mainly look at the protocol closely related to our Internet life: HTTP

What isHTTPProtocol

The full name of HTTP is Hypertext Transfer Protocal, that is, Hypertext Transfer Protocol, which has been widely used on WWW since 1990. It is the most widely used protocol on WWW today, and HTTP is the application layer protocol, when you browse the Web page online, the browser and the Web server send and receive data over the Internet over HTTP. HTTP is a stateless protocol based on the request/response mode. This is what we usually call request/response.

 

URL:

The URL (Uniform Resource Locator) address is used to describe resources on a network. The basic format is as follows:

Schema: // host [: Port #]/path /.../[? Query-String] [# anchor]

Scheme specifies the protocol used at the lower layer (for example, HTTP, https, and FTP)

IP address or domain name of the Host HTTP Server

Port # The default port number of the HTTP server is 80. In this case, the port number can be omitted. If another port is used, you must specify, for example, http://www.cnblogs.com: 8080/

Path

Data sent from query-string to the HTTP server

Anchor-anchor

 

URL example

Http://www.mywebsite.com/sj/test/test.aspx? Name = sviergn & X = true # stuff

Schema: HTTP
HOST: www.mywebsite.com
Path:/SJ/test. aspx
Query string: Name = sviergn & X = true
Anchor: stuff

 

HTTPOfRequest/Response:

First, let's look at the structure of the request message. The request message is divided into three parts:

The first part is request line,

The second part is request header,

The third part is that there is a blank line between body. header and body,

The structure is as follows:

 

The method in the first line indicates the request method. For example, "Post", "get", path-to-Resoure indicates the requested resource, and HTTP/version-number indicates the HTTP protocol version.

When the "get" method is used, the body is empty.

For example, the request for opening the blog homepage is as follows:

Get http://www.cnblogs.com/https/1.1
HOST: www.cnblogs.com

Abstract things are hard to understand. The old feeling is false. We can understand and remember what we see in reality. We use fiddler today. Let's take a look at the request and response.

Next we will start Fiddler to capture a request logged on to the blog Park and analyze its structure. On the Inspectors tab, we can see the complete request message in RAW mode,

For example

 

Accept

Role: acceptable media types on the browser,

For example, accept: text/html indicates that the browser can accept text/html, which is also a common HTML document,

If the server cannot return text/HTML data, the server should return a 406 error (non acceptable)

Wildcard * represents any type

For example, accept: */* indicates that the browser can process all types of data. (generally, this is what the browser sends to the server)

Referer:

Purpose: The server that provides the request context information to tell the server which link I came from, for example, from my homepage to a friend, his server can calculate from HTTP Referer how many users click the link on my homepage to visit his website every day.

Example: Referer: http://translate.google.cn /? Hl = ZH-CN & tab = WT

Accept-Language

Purpose: The browser declares the language it receives.

Differences between a language and a character set: Chinese is a language, and Chinese has multiple character sets, such as big5, gb2312, and GBK;

Example: Accept-language: En-US

Content-Type

Purpose:

Example: Content-Type: Application/X-WWW-form-urlencoded

 

Accept-Encoding:

Purpose: The browser declares the encoding method it receives. It usually specifies the compression method, whether compression is supported, and what compression method (gzip and deflate) is supported. (Note: This is not only character encoding );

Example: Accept-encoding: gzip, deflate

User-Agent

Purpose: Tell the HTTP server the name and version of the operating system and browser used by the client.

When we log on to the forum online, we will often see some welcome information, which lists the names and versions of your operating system, the names and versions of your browsers, this often makes many people feel amazing. In fact, server applicationsProgramThis information is obtained from the User-Agent Request Header domain. The User-Agent Request Header domain allows the client to tell the server its operating system, browser, and other attributes.

Example: User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CBA ;. net CLR 2.0.50727 ;. net CLR 3.0.20.6.2152 ;. net CLR 3.5.30729 ;. net4.0c; infopath.2 ;. net4.0e)

Connection

For example, connection: keep-alive when a webpage is opened, the TCP connection between the client and the server for transmitting HTTP data will not be closed. If the client accesses the webpage on this server again, will continue to use this established connection

For example, connection: Close indicates that after a request is completed, the TCP connection used to transmit HTTP data between the client and the server is closed. When the client sends the request again, a TCP connection needs to be established again.

Content-Length

Purpose: The length of the data sent to the HTTP server.

Example: Content-Length: 38

Host(This header field is required when a request is sent)

Purpose: The request header field is used to specify the Internet host and port number of the requested resource. It is usually extracted from the HTTP URL

For example, we enter: http://www.guet.edu.cn/index.html in the browser

The request message sent by the Browser contains the host Request Header domain, as follows:

HOST: http://www.guet.edu.cn

The default port number 80 is used here. If the port number is specified, it is changed to: Host: Specifies the port number.

Pragma

Purpose: prevent the page from being cached. In HTTP/1.1, it works exactly as well as cache-control: No-cache.

There is only one usage for pargma, for example: Pragma: No-Cache

COOKIE:

Role: The most important header that sends the cookie value to the HTTP server.

Accept-charset

Purpose: The browser declares the character set it receives. This is the various character sets and character encoding described earlier in this article, such as gb2312 and UTF-8 (we generally say charset includes the corresponding character encoding scheme );

 

 

 

Let's look at the structure of the response message, which is basically the same as the structure of the request message. It is also divided into three parts

The first part is response line,

The second part is response header,

The third part is that there is an empty line between body. header and body,

The structure is as follows:

 

HTTP/version-number indicates the HTTP protocol version. For details about status-code and message, see [Status Code.

We use Fiddler to capture the response of a blog homepage and analyze its structure. Under the Inspectors tab, we can see the complete response message in RAW mode, as shown in figure

 

Cache-control

Role: This is a very important rule. This is used to specify the cache mechanism followed by response-request. The meaning of each instruction is as follows:

Cache-control: public can be cached by any cache ()

Cache-control: Private content is only cached in the private cache.

Cache-control: No-Cache all content will not be cached

There are other usage cases. I do not understand the meaning. Please refer to other documents.

Content-Type

Purpose: The Web server informs the browser of the type and character set of the object to respond,

For example:

Content-Type: text/html; charset = UTF-8

Content-Type: text/html; charset = gb2312

Content-Type: image/JPEG

Expires

Purpose: The browser uses the local cache within the specified expiration time.

Example: expires: Tue, 08 Feb 2022 11:35:14 GMT

Last-modified:

Purpose: indicates the last modification date and time of the resource. (See the IF-modified-since instance in the example)

Example: Last-modified: Wed, 21 Dec 2011 09:09:10 GMT

Server:

Purpose: Specify the software information of the HTTP server.

For example, server: Microsoft-IIS/7.5

X-ASPnet-version:

Purpose: If the website is developed using ASP. NET, this header is used to indicate the version of ASP. NET.

Example: X-ASPnet-version: 4.0.30319

X-powered-:

Purpose: indicates the technology used for website development.

Example: X-powered-by: ASP. NET

Connection

For example, connection: keep-alive when a webpage is opened, the TCP connection between the client and the server for transmitting HTTP data will not be closed. If the client accesses the webpage on this server again, will continue to use this established connection

For example, connection: Close indicates that after a request is completed, the TCP connection used to transmit HTTP data between the client and the server is closed. When the client sends the request again, a TCP connection needs to be established again.

Content-Length

Specifies the length of the Object Body, expressed in decimal digits stored in bytes. In the process of data downlink, the Content-Length Method needs to cache all data on the server in advance, and then all the data is sent to the client.

Example: Content-Length: 19847

Date

Purpose: specify the time and date when the message is generated.

Example: Date: sat, 11 Feb 2012 11:35:14 GMT

HTTPAgreementGetAndPost

The HTTP protocol defines many methods to interact with the server. There are four basic methods: Get, post, put, and delete. a URL address is used to describe resources on a network. The get, post, put, and delete operations in HTTP correspond to four operations to query, modify, add, and delete resources. The most common ones are get and post. Get is generally used to obtain/query resource information, while post is used to update resource information.

Let's look at the difference between get and post.

1. The data submitted by get will be placed after the URL? Splits the URL and transmits data. parameters are connected with each other, for example, editposts. aspx? Name = test1 & id = 123456. The post method places the submitted data in the body of the http package.

2. The size of the data submitted by get is limited (because the browser has a limit on the URL length), but there is no limit on the data submitted by the POST method.

3. request is required for the get method. querystring to get the value of the variable, while the POST method uses request. form to get the value of the variable, that is, get transmits the value through the address bar, and post transmits the value through the submission form.

4. if you submit data in get mode, security issues may occur. For example, when you submit data in get mode on a login page, the user name and password will appear on the URL, if the page can be cached or accessed by others, you can obtain the user's account and password from the history.

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.