GoWeb HTTP protocol

Introduction to the HTTP protocol

HTTP Hypertext Transfer Protocol (HTTP-Hypertext transfer protocol), a part of object-oriented application layer protocol, since the device simple, fast way for distributed hypermedia information system. It is proposed in 1990, after several years of use and development, has been continuously improved and expanded. It is a rule details the mutual communication between the browser and the Web server , file transfer data transmission protocol of the World Wide Web via the Internet

Content delivery client and server communication called packets

HTTP is a communication rule which specifies the client sends to the server message format, but also provides the server to the client's message format.
The client sends to the server is called a request packet
sent by the server to the client is referred to as response packets

HTTP protocol development process

Formerly known as Hypertext Transfer Protocol is Xanadu (Xanadu) project, the concept of hypertext is泰德纳尔森(Ted Nelson) proposed in the 1960s. He has been committed to research and hypertext protocol of the project, but the information never published before.
In 1989, Tim Berners-Lee (Tim Berners Lee) as a software consultant at CERN (CERN = European Organization for Nuclear Research) time, developed a set of procedures, laid the World Wide Web (WWW = World Wide Web ) basis .
December 1990, for the first time on-line hypertext in CERN.
The summer of 1991, following protocols such as Telnet, Hypertext Transfer Protocol become part of many Internet protocols

At that time, Telnet protocol addresses the requirements of the control type communication between one computer and another computer.
Mail agreement resolves a sender communication requirements of a small amount of information to staff.
File transfer protocol to resolve a computer acquires the communication requirements of a file from another computer batch, but it does not have side while acquiring the file or files to the file function of some processing.
News Transfer Protocol solves the communication requirements of many news broadcasts.
The hypertext to solve the communication requirements are: acquiring and displaying stored in multiple computers in the text, data, images and other types of files on a single computer; it consists of two parts: hypertext transfer protocol and the Hypertext Markup language (HTML).
born HTTP, HTML and the browser popularity of the Internet has brought to leap

Session of the HTTP protocol

  • Communication process between the browser and the server to go through four steps

Here Insert Picture Description

  • WEB browser and the server connection process is short, and each handles only one connection request and response. Access to each page, the browser and the WEB server must establish a separate connection
  • All communication between the browser to the WEB server is completely independent and separate from the response to the request

HTTP1.0 and HTTP1.1 difference

In HTTP1.0 version, the browser requests a web page with pictures, download pictures and due to open a new connection between the server and
but in HTTP1.1 version, allows the browser to get all of the current in the corresponding request resources and then disconnect and improve efficiency

Here Insert Picture Description

HTTP1.1 is currently the most widely used version, but the latest version is HTTP2.0, also known as the HTTP / 2 on the open Internet HTTP2.0 will only be used https: // URL HTTPS, namely SSL (Secure. HTTP socket layer, secure sockets layer) above, in fact, HTTP communication in an upper layer SSL / TLS connections

Note: SSL was originally developed by Netscape, then taken over by the IETF (Internet Engineering Task Force, Internet Engineering Task Force) and renamed it TLS (Transport Layer Security, Transport Layer Security)

Packets

Message format

Here Insert Picture Description

Request packet

  1. Message format
  • Request line
  • Request header
  • Blank line
  • Request body
  1. GET request
  • GET request is not the request body, POST requests only request body
  • The HTTP /Hello/index.jsp the GET / 1.1 : the GET request, the request server path /Hello/index.jsp, agreement 1.1
  • Host: localhost host name localhost request
  • User-Agent: Mozilla / 4.0 ( compatible; MSIE 8.0 ...) associated with the browser and the OS information. Some sites will display version and system version information of the user's browser, this is by obtaining information from the User-Agent header of
  • Accept: * / * tell the server, document type for the current client can receive, * / * represents what can be received
  • Accept-Language: zh-CN current client support languages, you can browser Tools -> Language find information about the options
  • Accept-Encoding: gzip, deflate supported compression formats. When data is transmitted over the network, the server may then send the data compression will
  • Connection: keep-alive client supports linking, connecting a period of time, the default is 3000ms
  • Cookie: JSESSIONID = 369766FDF6220F7803433C0B2DE36D98 because it is not the first visit this address, it will be the last server in response to the request sent by Cookie together in the request sent in the past
  1. POST request
    POST request method requires the property to form a tag post
  • Referer: http: // localhost: 8080 / hello / index.jsp request pages from which, for example, you click on the link on Baidu to here, then Referer: http://www.baidu.com If you are in the browser's address direct input of the address bar, then there is no Referer request header of this
  • Content-Type: application / x- www-form-urlencoded form data type, will be described using the format of the encoded data url; url-encoded data is a "%" prefix, followed by two hexadecimal, e.g. " encoding "url word using UTF-8 encoding is"% E7% BC% 96% E7% A0% 81 "
  • Content-Length: 13 request length thereof, this is 13 bytes
  • keyword = hello request body content! hello data is entered in a form, keyword is the name of the form fields

web server

package main

import (
	"fmt"
	"net/http"
)

//创建处理器函数
func handler(w http.ResponseWriter, r *http.Request)  {
	fmt.Fprintln(w, "测试http协议!")
}
func main() {
	//调用处理器处理请求
	http.HandleFunc("/http", handler)
	//路由
	http.ListenAndServe(":8080", nil)
}

Test page

<html>
    <head>
        <meta charset="UTF-8" />
    </head>
    <body>
        <form action="http://localhost:8080/http" method="POST">
            用户名: <input type="text" name="username" /><br/>
            密码: <input type="password" name="password" /><br/>
            <input type="submit" />
        </form>
    </body>
</html>

Response message

  1. Message format
  • Response line
  • Response header
  • Blank line
  • Response body

Response message parsing

  • HTTP / 1.1 200 OK response to the HTTP1.1 protocol, status code 200 indicating the request was successful
  • Server: Apache-Coyote / 1.1 version Information Server
  • Content-Type: text / html; charset = UTF-8 encoding for the response member using UTF-8
  • Content-Length: 274 in response to body 274 bytes
  • Date: Tue, 07 Apr 2015 10:08:26 GMT response time may be from 8-hour time zone difference

The response status code

  1. Status code tells the HTTP client, the HTTP server results expected Response HTTP / 1.1 protocol status code defines the class 5, three-digit status code, first defined response categories
  • 1XX represents information - indicates that the request has been successfully received, processing continues
  • 2XX success - indicates that the request has been successfully received, understood, accepted
  • 3XX redirection - the request must be completed for further processing
  • 4XX Client Error - The request has a syntax error or a request can not be achieved
  • 5XX server-side error - server failed to achieve a legitimate request
  1. Response code is important for browsers that tells the browser in response to the results, a common status codes are:
  • 200 successful request, the browser content body will respond (usually html) displayed in the browser
  • 404 requested resource is not found, indicating that the client requests a resource error does not exist
  • 500 requested resource is found, but the internal server error occurred
  • 302 redirect response code if the URL address is 302, indicating that the server requires a browser to re-send the request, the server sends a response header Location, it is assigned a new request
Published 116 original articles · won praise 27 · views 10000 +

Guess you like

Origin blog.csdn.net/wuxingge/article/details/105223204