Home  >  Article  >  Web Front-end  >  Detailed introduction to restful http

Detailed introduction to restful http

零下一度
零下一度Original
2017-07-24 14:20:271596browse
HTTP (HyperText Transfer Protocol) is a set of rules for computers to communicate through the network. Computer experts designed HTTP to enable HTTP clients (such as Web browsers) to request information and services from HTTP servers (Web servers). The current version of the HTTP protocol is 1.1. HTTP is a stateless protocol, and stateless refers to the Web There is no need to establish a persistent connection between the browser and the Web server. This means that when a client makes a request to the server and the Web server returns a response, the connection is closed and no information about the connection is retained on the server. Information.HTTP follows the request/response model. The web browser sends a request to the web server, and the web server processes the request and returns an appropriate response. All HTTP connections are structured as a set of requests and responses.
HTTP uses content types, which means that the files returned by the web server to the web browser have related types. All of these types are modeled on the MIME Internet Mail Protocol, which means that the web server tells the web browser what kind of file it is, whether it is an HTML document, a GIF image, a sound file, or a standalone application. Most Web browsers have a set of configurable helper applications that tell the browser how to handle various content types sent by the Web server.
The HTTP communication mechanism is that during a complete HTTP communication process, the following 7 steps will be completed between the web browser and the web server:
(1) Establish a TCP connection
Before HTTP work begins, the web browser must first establish a connection with the web server through the network. The connection is completed through TCP. This protocol and the IP protocol jointly build the Internet, the famous TCP/IP protocol family, so the Internet Also known as TCP/IP network. HTTP is a higher-level application layer protocol than TCP. According to the rules, only after the lower-layer protocol is established, the higher-layer protocol can be connected. Therefore, a TCP connection must be established first. Generally, the port number of the TCP connection is 80
(2) The web browser sends a request command to the web server
Once the TCP connection is established, the web browser will send a request command to the web server
For example: GET/sample/hello .jsp HTTP/1.1
(3) The Web browser sends the request header information
After the browser sends its request command, it also sends some other information to the Web server in the form of header information. , and then the browser sends a blank line to notify the server that it has finished sending the header information.
(4) Web server response
After the client sends a request to the server, the server will send a response back to the client,
HTTP/1.1 200 OK
The first part of the response is the protocol version number and response status code
(5) The Web server sends response header information
Just as the client will send information about itself along with the request, the server will also Along with the response, the user is sent data about itself and the requested document.
(6) The Web server sends data to the browser
After the Web server sends the header information to the browser, it will send a blank line to indicate that the sending of the header information ends here, and then , it sends the actual data requested by the user in the format described by the Content-Type response header information
(7) The Web server closes the TCP connection
Under normal circumstances, once the Web server sends a request to the browser After the server sends the request data, it will close the TCP connection, and then if the browser or server adds this line of code to its header information
Connection:keep-alive
After the TCP connection is sent will remain open so the browser can continue sending requests over the same connection. Keeping connections saves the time required to establish a new connection for each request and also saves network bandwidth.
  
HTTP request format
When the browser makes a request to the Web server, it passes a data block to the server, which is the request information. The HTTP request information consists of 3 Part composition:
l Request method URI protocol/version
l Request header (Request Header)
l Request body
The following is an HTTP request Example:
GET/sample.jspHTTP/1.1
Accept:image/gif.image/jpeg,*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
Accept- Encoding:gzip,deflate
username=jinqiao&password=1234
(1) Request method URI protocol/version
request The first line is "Method URL proposal/version": GET/sample.jsp HTTP/1.1
In the above code, "GET" represents the request method, "/sample.jsp" represents the URI, "HTTP/1.1
Represents the protocol and protocol version.
According to the HTTP standard, HTTP requests can use multiple request methods. For example: HTTP1.1 supports 7 request methods: GET, POST, HEAD, OPTIONS, PUT, DELETE and TARCE. In Internet applications, the most commonly used methods are GET and POST.
The URL completely specifies the network resource to be accessed, usually just giving a relative directory relative to the root directory of the server, so it always starts with "/", and finally, the protocol version declares the communication process version using HTTP.
(2) Request Header
The request header contains a lot of useful information about the client environment and the request body. For example, the request header can declare the language used by the browser, the length of the request body, etc.
Accept:image/gif.image/jpeg.*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host :localhost
User-Agent:Mozila/4.0(compatible:MSIE5.01:Windows NT5.0)
Accept-Encoding:gzip,deflate.
(3) Request body
There is a blank line between the request header and the request body. This line is very important. It indicates that the request header has ended, and what follows is the request body. The request body can contain the query string information submitted by the customer:
username=jinqiao&password=1234
In the above example of the HTTP request, the request body has only one line of content. Of course, in actual applications, the HTTP request body can contain more content.
HTTP request method I will only discuss the GET method and POST method here
l GET method
The GET method is the default HTTP request method. We use the GET method to submit forms every day. Data, however, the form data submitted using the GET method is only briefly encoded, and it will be sent to the Web server as part of the URL. Therefore, if you use the GET method to submit the form data, there are security risks. For example
Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB
From the above URL request, it is easy to Recognize the content of form submissions. (What follows?) In addition, since the data submitted by the GET method is part of the URL request, the amount of data submitted cannot be too large
l   POST method
The POST method is an alternative to the GET method Method, it mainly submits form data to the Web server, especially large batches of data. The POST method overcomes some of the shortcomings of the GET method. When submitting form data through the POST method, the data is not transmitted to the Web server as part of the URL request but as standard data. This overcomes the shortcomings of the GET method in which the information cannot be kept confidential and the data volume is too small. Therefore, for security reasons and respect for user privacy, the POST method is usually used when submitting forms.
From a programming perspective, if the user submits data through the GET method, the data is stored in the QUERY_STRING environment variable, while the data submitted by the POST method can be obtained from the standard input stream.
HTTP response is similar to HTTP request. HTTP response also consists of 3 parts, namely:
l Protocol status version code description
l Response header (Response Header)
l Response text
The following is an example of an HTTP response:
HTTP/1.1 200 OK
Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:23:42 GMT
Content-Length:112
 

HTTP响应示例<title>
</head>
<body>
Hello HTTP!
</body>
</html>协议状态代码描述HTTP响应的第一行类似于HTTP请求的第一行,它表示通信所用的协议是HTTP1.1服务器已经成功的处理了客户端发出的请求(200表示成功):
HTTP/1.1 200 OK响应头(Response Header)响应头也和请求头一样包含许多有用的信息,例如服务器类型、日期时间、内容类型和长度等:
   Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:13:33 GMT
Content-Type:text/html
Last-Moified:Mon,6 Oct 2003 13:23:42 GMT
Content-Length:112
 响应正文响应正文就是服务器返回的HTML页面:
  <html><head>
<title>HTTP响应示例<title>
</head>
<body>
Hello HTTP!
</body>
</html></pre></div><div>The response header and the body must also be Separate with blank lines.​ </div><div>l​​​ HTTP response code</div><div> HTTP response code is also called status code, which reflects the status of the Web server processing HTTP requests. The HTTP response code consists of 3 digits, where the first digit defines the type of response code: </div><div> 1XX - Information (Information), indicating that a web browser request has been received and is being processed further</div><div> 2XX-Successful, indicating that the user request was correctly received, understood and processed. For example: 200 OK</div><div> 3XX-Redirection, indicating that the request was not successful and the customer must take further action . </div><div> 4XX-Client Error (Client Error), indicating that the request submitted by the client has an error. For example: 404 NOT</div><div>                       Found, which means that the document referenced in the request does not exist. </div><div> 5XX-Server Error (Server Error) means that the server cannot complete the processing of the request: such as 500</div><div> For us web developers, mastering HTTP response codes can help improve web application debugging efficiency and accuracy. </div><div> </div><div>Secure connection</div><div>One of the most common uses of Web applications is e-commerce, which can use Web server-side programs to enable people to shop online. It needs to be pointed out that by default, sending information over the Internet is not secure. If someone happens to How bad would it be if a message you sent to a friend was intercepted and he could open it and imagine it contained your credit card number? Fortunately, many web servers and web browsers have the ability to create secure connections. , so that they can communicate safely. </div><div>The most common standard for providing secure connections over the Internet is the Secure Sockets layer (SSl) protocol. The SSL protocol is an application layer protocol (like HTTP) used to exchange data on the Web in a secure manner. SSL uses a public key encoding system. Essentially, this means that each party in the business has a public and a private key. When one party encodes using the other party's public key, only the person with the matching key can decode it. Simply put, public key encoding provides a secure method for exchanging data between two parties. After the SSL connection is established, both the client and the server exchange public keys and verify them before making business contact. Once both parties Once the keys are verified, data can be exchanged securely. </div></div><div><ul class=" list-paddingleft-2"><li><p>GET<br>Get the resource by <strong> requesting the </strong>URI</p></li><li><p>POST,<br> Used to add new content</p></li><li><p>PUT<br>Used to modify a certain content</p></li><li>##DELETE,<p>Delete a certain content<br></p></li><li>CONNECT,<p> is used for proxy transmission, such as using SSL<br></p></li>##OPTIONS<li>Ask which <p> methods can be executed<br><strong></strong></p></li>PATCH,<li>Partial document changes<p><br></p></li>PROPFIND, (wedav)<li>View properties<p><br> </p></li>PROPPATCH, (wedav)<li>Set properties<p><br></p></li>MKCOL, (wedav)<li>Create a collection (folder)<p><br></p></li>COPY, (wedav)<li>Copy<p><br></p></li>MOVE, (wedav)<li>Move<p><br></p></li>LOCK, (wedav )<li>Lock<p><br></p></li>UNLOCK (wedav)<li>Unlock<p><br></p></li>TRACE<li>For remote diagnosis server<p> <br></p></li>HEAD<li>Similar to GET, but does not return body information. It is used to check whether the object exists and get the metadata of the object<p><br></p></li></ul></div></body></html>
<p>The above is the detailed content of Detailed introduction to restful http. For more information, please follow other related articles on the PHP Chinese website!</p></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="jQuery provides two methods for developing plug-ins" href="http://m.php.cn/faq/373130.html">jQuery provides two methods for developing plug-ins</a></span><span>Next article:<a class="dBlack" title="jQuery provides two methods for developing plug-ins" href="http://m.php.cn/faq/373132.html">jQuery provides two methods for developing plug-ins</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/347615.html" title="jQuery waterfall absolute positioning layout (2) (delayed AJAX loading of images)" class="aBlack">jQuery waterfall absolute positioning layout (2) (delayed AJAX loading of images)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/347616.html" title="jQuery waterfall flow floating layout (1) (delayed AJAX loading of images)" class="aBlack">jQuery waterfall flow floating layout (1) (delayed AJAX loading of images)</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/347619.html" title="Learn more about js waterfall flow layout" class="aBlack">Learn more about js waterfall flow layout</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/347665.html" title="Deep copy of objects in JavaScript" class="aBlack">Deep copy of objects in JavaScript</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/347666.html" title="Detailed explanation of shallow copy and deep copy in javascript" class="aBlack">Detailed explanation of shallow copy and deep copy in javascript</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai"  >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs"  ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a  onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan "  href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a  onclick="javascript:;" class="language course-right-orders chooselan chooselanguage"  href="javascript:;"><span>English</span><span>(EN)</span></a><a  onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan "  href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a></div></div><script>            var swiper = new Swiper('.nphpNavSwiper1', {
                slidesPerView : 'auto',
                observer: true,//修改swiper自己或子元素时,自动初始化swiper
                observeParents: true,//修改swiper的父元素时,自动初始化swiper

            });
        </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) {
    var _times = times || -1, //100次
    _interval = interval || 20, //20毫秒每次
    _self = this,
    _selector = this.selector, //选择器
    _iIntervalID; //定时器id
    if( this.length ){ //如果已经获取到了,就直接执行函数
        func && func.call(this);
    } else {
        _iIntervalID = setInterval(function() {
            if(!_times) { //是0就退出
                clearInterval(_iIntervalID);
            }
            _times <= 0 || _times--; //如果是正数就 --

            _self = $(_selector); //再次选择
            if( _self.length ) { //判断是否取到
                func && func.call(_self);
                clearInterval(_iIntervalID);
            }
        }, _interval);
    }
    return this;
}
$("table.syntaxhighlighter").wait(function() {
    $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>");
});
$(document).on("click", ".cnblogs_code_footer",function(){
      $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide();
});
$('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}});
</script></body></html>