HTTP methods and protocols

What is HTTP?

The Hypertext Transfer Protocol (HTTP) was designed to ensure communication between clients and servers.

The way HTTP works is a request-response protocol between client and server.

A web browser may be the client, and a web application on your computer may be the server.

Example: The client (browser) submits an HTTP request to the server; the server returns a response to the client. The response contains status information about the request and what may have been requested.

 

【Stateless Protocol】

In the HTTP protocol, the client always initiates the request and the server sends back the response.

This limits the use of the HTTP protocol, and it is impossible for the server to push messages to the client when the client does not initiate a request.

The HTTP protocol is a stateless protocol, and this request from the same client has no correspondence with the previous request.

HTTP is an application layer protocol consisting of requests and responses, and is a standard client-server model. HTTP is a stateless protocol.

 

【Version History】

Hypertext Transfer Protocol has evolved many versions, most of them are backward compatible.

 

[HTTP source and function:]

All WWW documents must comply with this standard. HTTP was originally designed to provide a way to publish and receive HTML pages. HTTP is a client-side and server-side request and response standard (TCP). The client is the end user and the server is the website. Using a web browser, web crawler, or other tool, the client initiates an HTTP request to the server on the specified port (the default port is 80). (We call this client) a user agent. The responding server has (some) resources stored on it, such as HTML files and images. (We call) this answering server the origin server. There may be multiple intermediaries between the user agent and the origin server, such as proxies, gateways, or tunnels. Although the TCP/IP protocol is the most popular application on the Internet, the HTTP protocol does not dictate that it must be used and (based on) the layers it supports. In fact, HTTP can be implemented over any other internet protocol, or over other networks. HTTP only assumes (and its underlying protocols provide) reliable transport, and any protocol that can provide this guarantee can be used by it.

Typically, an HTTP client initiates a request to establish a TCP connection to the server's specified port (port 80 by default). The HTTP server listens on that port for requests sent by the client. Once the request is received, the server sends back (to the client) a status line, such as "HTTP/1.1 200 OK", and a (response) message whose body may be the requested file, an error message, or some other information .

The reason HTTP uses TCP instead of UDP is that (to open) a web page must transmit a lot of data, and the TCP protocol provides transmission control, organizing data in order, and error correction.

 

[URL function:]

Identifies the resource requested by the HTTP or HTTPS protocol

 

【HTTP protocol function】

The HTTP protocol (HyperText Transfer Protocol) is a transfer protocol for transferring hypertext from a WWW server to a local browser. It can make the browser more efficient and reduce network traffic. It not only ensures that the computer transmits hypertext documents correctly and quickly, but also determines which part of the document is transmitted, and which part of the content is displayed first (eg, text before graphics).

HTTP is an application-layer communication protocol between a client browser or other program and a web server.

All hypertext information is stored on the Web server on the Internet, and the client needs to transmit the hypertext information to be accessed through the HTTP protocol. HTTP contains commands and transmission information, which can be used not only for Web access, but also for communication between other Internet/Intranet application systems, so as to realize the integration of hypermedia access to various application resources.

The website address we enter in the browser's address bar is called a URL (Uniform Resource Locator). Just like every household has a house number address, every web page also has an Internet address. when you are

When you enter a URL in the browser's address box or click a hyperlink, the URL determines the address to browse. The browser extracts the web page code of the site on the web server through the Hypertext Transfer Protocol (HTTP) and translates it into a beautiful web page.

 

【Agreement Basics】

HTTP(HyperText Transport Protocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616。HTTP协议采用了请求/响应模型。

客户端向服务器发送一个请求,请求头包含请求的方法、URL、协议版本、以及包含请求修饰符、客户信息和内容的类似于MIME的消息结构。服务器以一个状态行作为响应,响应的内容包括消息协议的版本,成功或者错误编码加上包含服务器信息、实体元信息以及可能的实体内容。

通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息。

这两种类型的消息由一个起始行,一个或者多个头域,一个指示头域结束的空行和可选的消息体组成。HTTP的头域包括通用头,请求头,响应头和实体头四个部分。

每个头域由一个域名,冒号(:)和域值三部分组成。域名是大小写无关的,域值前可以添加任何数量的空格符,头域可以被扩展为多行,在每行开始处,使用至少一个空格或制表符。

 

【工作原理】

一次HTTP操作称为一个事务,其工作过程可分为四步:

①首先客户机与服务器需要建立连接。只要单击某个超级链接,HTTP的工作就开始了。

建立连接后

②客户机发送一个请求给服务器,请求方式的格式为:统一资源标识符(URL)、协议版本号,后边是MIME信息包括请求修饰符、客户机信息和可能的内容。

③服务器接到请求后,给予相应的响应信息,其格式为一个状态行,包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。

④客户端接收服务器所返回的信息通过浏览器显示在用户的显示屏上,然后客户机与服务器断开连接。

 

    如果在以上过程中的某一步出现错误,那么产生错误的信息将返回到客户端,由显示屏输出。对于用户来说,这些过程是由HTTP自己完成的,用户只要用鼠标点击,等待信息显示就可以了。

    许多HTTP通讯是由一个用户代理初始化的并且包括一个申请在源服务器上资源的请求。最简单的情况可能是在用户代理和服务器之间通过一个单独的连接来完成。在Internet上,HTTP通讯通常发生在TCP/IP连接之上。缺省端口是TCP 80,但其它的端口也是可用的。但这并不预示着HTTP协议在Internet或其它网络的其它协议之上才能完成。HTTP只预示着一个可靠的传输。

    这个过程就好像我们打电话订货一样,我们可以打电话给商家,告诉他我们需要什么规格的商品,然后商家再告诉我们什么商品有货,什么商品缺货。这些,我们是通过电话线用电话联系(HTTP是通过TCP/IP),当然我们也可以通过传真,只要商家那边也有传真。

 

【两种最常用的 HTTP 方法是:GET 和 POST】

在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST

①GET - 从指定的资源请求数据,在请求的URL中发送

②POST - 向指定的资源提交要被处理的数据,在HTTP请求主体中发送

(1)GET 方法

请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:

/test/demo_form.asp?name1=value1&name2=value2

有关 GET 请求的其他一些注释:

GET 请求可被缓存

GET 请求保留在浏览器历史记录中

GET 请求可被收藏为书签

GET 请求不应在处理敏感数据时使用

GET 请求有长度限制

GET 请求只应当用于取回数据

(2)POST方法

请注意,查询字符串(名称/值对)是在 POST 请求的 HTTP 消息主体中发送的:

POST /test/demo_form.asp HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2

有关 POST 请求的其他一些注释:

POST 请求不会被缓存

POST 请求不会保留在浏览器历史记录中

POST 不能被收藏为书签

POST 请求对数据长度没有要求

 

【Compare GET and POST】

The table below compares two HTTP methods: GET and POST

  GET POST
back button/refresh harmless The data will be resubmitted (the browser should inform the user that the data will be resubmitted).
bookmark bookmarkable Not bookmarked
cache can be cached cannot be cached
encoding type application/x-www-form-urlencoded application/x-www-form-urlencoded or multipart/form-data. Use multiple encodings for binary data.
history Parameters are preserved in the browser history. Parameters are not saved in browser history.
restrictions on data length Yes. When sending data, the GET method adds data to the URL; the length of the URL is limited (the maximum URL length is 2048 characters). Unlimited.
Restrictions on data types Only ASCII characters are allowed. no limit. Binary data is also allowed.
safety

GET is less secure than POST because the data sent is part of the URL.

Never use GET when sending passwords or other sensitive information!

POST is more secure than GET because parameters are not saved in browser history or web server logs.
visibility The data is visible to everyone in the URL. Data will not be displayed in the URL.

 

[Other HTTP request methods]

The following table lists some other HTTP request methods:

Method description
HEAD Same as GET, but returns only the HTTP headers, not the document body.
PUT Upload the specified URI representation.
DELETE Delete the specified resource.
OPTIONS Returns the HTTP methods supported by the server.
CONNECT Convert the request connection to a transparent TCP/IP channel.

 

 

 

 

 

[Extension: HTTP Proxy Server]

(1) Introduction and composition

The full name of the proxy server in English is Proxy Server, and its function is to proxy network users to obtain network information. Figuratively speaking: it is a transfer station for network information.

代理服务器是介于浏览器和Web服务器之间的一台服务器,有了它之后,浏览器不是直接到Web服务器去取回网页而是向代理服务器发出请求,Request信号会先送到代理服务器,由代理服务器来取回浏览器所需要的信息并传送给你的浏览器。

而且,大部分代理服务器都具有缓冲的功能,就好象一个大的Cache,它有很大的存储空间,它不断将新取得数据储存到它本机的存储器上,如果浏览器所请求的数据在它本机的存储器上已经存在而且是最新的,那么它就不重新从Web服务器取数据,而直接将存储器上的数据传送给用户的浏览器,这样就能显著提高浏览速度和效率。

更重要的是:Proxy Server(代理服务器)是Internet链路级网关所提供的一种重要的安全功能,它的工作主要在开放系统互联(OSI)模型的对话层。

(2)http代理服务器的主要功能:

主要功能如下:

①突破自身IP访问限制,访问国外站点。如:教育网、169网等网络用户可以通过代理访问国外网站;

②访问一些单位或团体内部资源,如某大学FTP(前提是该代理地址在该资源的允许访问范围之内),使用教育网内地址段免费代理服务器,就可以用于对教育 网开放的各类FTP下载上传,以及各类资料查询共享等服务;

③突破中国电信的IP封锁:中国电信用户有很多网站是被限制访问的,这种限制是人为的,不同Serve对地址的封锁是不同的。所以不能访问时可以换一个国 外的代理服务器试试;

④提高访问速度:通常代理服务器都设置一个较大的硬盘缓冲区,当有外界的信息通过时,同时也将其保存到缓冲区中,当其他用户再访问相同的信息时, 则直接由缓冲区中取出信息,传给用户,以提高访问速度;

⑤隐藏真实IP:上网者也可以通过这种方法隐藏自己的IP,免受攻击。

(3)http代理图示:

 

 

【拓展:虚拟主机的实现】

(一)简介及构成:

虚拟主机:是在网络服务器上划分出一定的磁盘空间供用户放置站点、应用组件等,提供必要的站点功能与数据存放、传输功能。  

所谓虚拟主机,也叫“网站空间”就是把一台运行在互联网上的服务器划分成多个“虚拟”的服务器,每一个虚拟主机都具有独立的域名和完整的Internet服务器(支持WWW、FTP、E-mail等)功能。一台服务器上的不同虚拟主机是各自独立的,并由用户自行管理。但一台服务器主机只能够支持一定数量的虚拟主机,当超过这个数量时,用户将会感到性能急剧下降。

(二)原理:

虚拟主机是用同一个WEB服务器,为不同域名网站提供服务的技术。Apache、Tomcat等均可通过配置实现这个功能。

相关的HTTP消息头:Host。

例如:Host: www.baidu.com

客户端发送HTTP请求的时候,会携带Host头,Host头记录的是客户端输入的域名。这样服务器可以根据Host头确认客户要访问的是哪一个域名。

 

 

【URI】

先来对比下URL和URI的区别

具体详解我在后面文章里做了总结

 

【总结:】

①网页里文本优先于图片展示原因:HTTP协议可以确定传输文档中哪部分优先显示

②代理服务器:可以突破自身IP访问限制,访问国外站点,它的工作主要在开放系统互联(OSI)模型的对话层

③相对应的HTTP是客户端浏览器或其他程序与Web服务器之间的应用层通信协议

④虚拟主机:一台服务器主机只能够支持一定数量的虚拟主机,当超过这个数量时,用户将会感到性能急剧下降

⑤URL统一资源定位符,URI统一资源标识符

 

.

Guess you like

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