[17] on the HTTP protocol, one is enough

HTTP Profile

HTTP protocol is the Hyper Text Transfer abbreviation Protocol (Hypertext Transfer Protocol) is used from the World Wide Web (WWW: World Wide Web) server to transfer hypertext transfer protocol local browser.

HTTP is used to transfer a data (HTML files, image files, query results, etc.) based on TCP / IP communication protocol.

HTTP is a protocol belonging to the object-oriented application layer, due to its 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. Currently used in the WWW is the sixth version of the HTTP / 1.0, the standardization work underway in HTTP / 1.1, and HTTP-NG (Next Generation of HTTP) recommendations have been proposed.

HTTP protocol works on the client - server architecture on. Browser as an HTTP client URL that is WEB server sends all requests to the server via HTTP. Web server according to the received request, transmits the response information to the client.


http request - response model .jpg

main feature

1, simple and fast: a customer service request to the server, instead of sending the request method and path. Request method commonly used GET, HEAD, POST. Each method provides a different type of client contacts the server. Due to the simple HTTP protocol, HTTP server makes the program a small scale, so the communication is very fast.

2, flexible: HTTP allows the transmission of any type of data object. It is the type of transmission being marked by Content-Type.

3. No connection: Meaning No connection is to limit the processing only one request per connection. After the server processes client requests and receives the customer's response, i.e., disconnected. In this way it can save transmission time.

4. stateless: HTTP protocol is stateless protocol. No state is no protocol for transaction processing and memory. If the lack of state means that the subsequent processing required in front of the information, it must be retransmitted, which may result in the amount of data transmitted for each connection is increased. On the other hand, when it does not require previous information in response to a faster server.
5, supports the B / S and C / S Mode.

HTTP之URL

HTTP uses uniform resource identifier (Uniform Resource Identifiers, URI) to transmit data and establish a connection. URL is a special type of URI, it contains enough information for finding a resource

URL, stands for UniformResourceLocator, Chinese called a Uniform Resource Locator, is the address used to identify a resource on the Internet at. At the following URL as an example, each part of the composition of the next ordinary URL:

http://www.aspxfans.com:8080/news/index.asp?boardID=5&ID=24618&page=1#name

As can be seen from the above URL, a full URL includes the following sections:
1. Agreement: Protocol as part of the URL "http:", which represents the web page using the HTTP protocol. More may be used in the Internet protocols, such as HTTP, FTP and the like used in this example is the HTTP protocol. In the "HTTP" behind "//" is a delimiter

2. The domain name parts: the part of the URL of the domain name "www.aspxfans.com". A URL, or you can use the IP address as the domain name

Part 3. Port: following the domain name is to use between the ports, the domain name and port ":" as the delimiter. Port is not a mandatory part of the URL, if you omit part of the port, the default port

4. Virtual directory section: from the first domain name after "/" beginning to the last "/" so far, is the virtual directory section. Virtual directory is not a necessary part of the URL. In this example the virtual directory is "/ news /"

5. part of the file name: the domain name from the last "/" beginning to date, is part of the file name, if not, is the last of a domain name after "/" to start "#" so far "?" "?" , is part of the file, if there is no "?" and "#", then the domain name from the last "/" start to finish, it is part of the file name. In this case the file name is "index.asp". Part of the file name is not a necessary part of the URL, if you omit this part, the default file name

6. anchor parts: from the "#" beginning to end, are part of the anchor. Anchor of this embodiment is "name". Anchor part of the URL is not a necessary part

7. Parameters section: from the beginning to the part of the argument between the part until the "#", also known as part of the search query part "?." Parameters of this embodiment is "boardID = 5 & ID = 24618 & page = 1". Parameter to allow a plurality of parameters, with the parameters between the parameter and the "&" as the delimiter.

(Original: http://blog.csdn.net/ergouge/article/details/8185219 )

The difference between URI and URL

URI, is the uniform resource identifier, uniform resource identifier, used to uniquely identify a resource.

Each resource available on the Web such as HTML documents, images, video clips, programs are to a URI to locate the
URI is generally composed of three:
① access to the resource naming scheme
② storage resource host name
③ name of the resource itself , represented by the path, with emphasis on resources.

URL is a uniform resource locator, Uniform Resource Locator, which is a specific URI, namely URL can be used to identify a resource, but also indicates how locate this resource.

URL is a string used to describe the information resources on the Internet, mainly used in various WWW client and server programs, especially the famous Mosaic.
URL can adopt a uniform format to describe a variety of information resources, including file, the server's address and directories. URL is generally composed of three:
① agreement (otherwise known as service mode)
② there IP address of the host resources (sometimes including the port number)
specifically address ③ host resources. Such as directory and file name, etc.

URN, uniform resource name, uniform resource name, the resource is identified by name, such as mailto: [email protected].

URI is an abstract, high-level definition of the concept of uniform resource identifier, and the URL and URN are specific resources identified. URL and URN is a kind of URI. Broadly speaking, each URL is a URI, but not every URI is a URL. This is because the URI also includes a subclass, or Uniform Resource Name (URN), which named resource but does not specify how to locate resources. The above mailto, news, and examples are a URN isbn URI.

In Java's URI in a URI instance it can represent absolute or relative, as long as it meets the URI syntax rules. The URL class is not only in semantics, also contains information to locate the resources, so it can not be a relative.
In the Java class library, URI class does not contain any method to access the resource, its only function is to resolve.
In contrast, URL class can open a flow of resources to reach.

The HTTP request message Request

The client sends a request to the server HTTP request message comprises the following format:

Request line (request line), the request header (header), a blank line portion and four request data.




Http request message structure .png
  • The method begins with a request line symbol, separated by spaces, and followed the protocol version request URI.
Get request example, use Charles crawl request:
GET /562f25980001b1b106000338.jpg HTTP/1.1
Host    img.mukewang.com
User-Agent    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
Accept    image/webp,image/*,*/*;q=0.8
Referer    http://www.imooc.com/
Accept-Encoding    gzip, deflate, sdch
Accept-Language    zh-CN,zh;q=0.8
First part: the request line, for explaining the type of request to access the resource, and the version of HTTP being used.

GET Request type GET, [/ 562f25980001b1b106000338.jpg] as a resource to be accessed, the last part of the line of instructions is HTTP1.1 version.

Part II: request header, immediately after the request line portion (i.e., first row), for explaining the additional information server to be used

From the second row for the request header, HOST will indicate the destination .User-Agent, the server-side and client-side scripts can request access to it, it is an important basis for the type of browser detection logic. The information provided by your browser It is defined, and automatically sending each request, etc.

Part III: blank line, a blank line back to the request header is required

Even if a fourth request data section is empty, empty line must be.

Part IV: The request also called data subject, you can add any other data.

Examples of this request data is empty.

Examples POST request, using Charles crawl request:
POST / HTTP1.1
Host:www.wrox.com
User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Content-Type:application/x-www-form-urlencoded
Content-Length:40
Connection: Keep-Alive

name=Professional%20Ajax&publisher=Wiley

Part I: request lines, the first line is clear post request, and http1.1 version.
Part II: request header, the second to sixth rows.
Part III: blank lines, blank lines seventh row.
Part IV: request data, the eighth line.

The HTTP Response message in response to

In general, the server receives and processes requests sent, the client returns a response message of HTTP.

HTTP response also consists of four parts, namely: a status line, message header, and response body blank line.



http response message formats .jpg

example

HTTP/1.1 200 OK
Date: Fri, 22 May 2009 06:07:21 GMT
Content-Type: text/html; charset=UTF-8

<html>
      <head></head>
      <body>
            <!--body goes here-->
      </body>
</html>
Part I: status line, by the HTTP protocol version, status code, a status message of three parts.

A first status line behavior, (HTTP / 1.1) shows that HTTP version 1.1 version, status code 200, status message (ok)

Part II: message header, used to illustrate some additional information to be used by the client

The second and third behaviors message header,
a Date: the date and time response; Content-Type: Specifies the MIME type of HTML (text / html), encoding type is UTF-8

Part III: blank line, blank line after the message header is required
Part IV: response body, the server returns the text information to the client.

html part blank line after the response body.

The HTTP status codes

Status code has three numbers, the first number of defined categories of response, is divided into five categories:

1xx: indication information - indicates a request has been received, processing continues
2xx: Success - indicates that the request has been successfully received, understood, accepted
3xx: Redirection - to fulfill the request must go a step further
4xx: Client Error - The request has a syntax error or a request can not be achieved
5xx: Server-side Error - The server failed to achieve a legitimate request

Common status codes:

200 OK                        //客户端请求成功
400 Bad Request               //客户端请求有语法错误,不能被服务器所理解
401 Unauthorized              //请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用 
403 Forbidden                 //服务器收到请求,但是拒绝提供服务
404 Not Found                 //请求资源不存在,eg:输入了错误的URL
500 Internal Server Error     //服务器发生不可预期的错误
503 Server Unavailable        //服务器当前不能处理客户端的请求,一段时间后可能恢复正常

More state code http://www.runoob.com/http/http-status-codes.html

The method of HTTP request

The standard HTTP, HTTP request can use several request methods.
HTTP1.0 request defines three methods: GET, POST, and HEAD method.
HTTP1.1 five new request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT method.

GET     请求指定的页面信息,并返回实体主体。
HEAD     类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
POST     向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
PUT     从客户端向服务器传送的数据取代指定的文档的内容。
DELETE      请求服务器删除指定的页面。
CONNECT     HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
OPTIONS     允许客户端查看服务器的性能。
TRACE     回显服务器收到的请求,主要用于测试或诊断。

HTTP works

HTTP protocol defines how Web client-side requests a Web page from the Web server and the server how to transfer a Web page to the client. HTTP protocol uses a request / response model. The client sends the server a request packet, the method comprising the requested request packet, URL, protocol version, and requested data request header. A server response status line, including the contents of the protocol version response, success or error code, the server information, the response header and the response data.

The following is a step of HTTP request / response:

1, the client connects to the Web server

An HTTP client, typically a browser, Web server with HTTP port (default is 80) to establish a TCP socket connection. For example, http://www.oakcms.cn.

2, transmits an HTTP request

Sending a TCP socket text, client request message to the Web server, a request packet by the request line, request headers, blank lines, and 4 parts request data.

3, the server accepts the request and returns an HTTP response

Web server parses request, locate the requested resource. The copy of the resource server writes a TCP socket, read by the client-side. A response line from the state, in response to the head, and blank lines in response to data 4 parts.

4, releases the connection TCP connection

When the connection mode is close, the server closes the TCP connection , the client closes the connection passive release the TCP connection ; if Keepalive connection mode, then the connection will be maintained for a period of time, you can continue to receive requests in the time;

5, the client browser parses the HTML content

The client browser status line resolved first, to see that the request is successful status code. Then parse each response header, the response header to inform the character set of HTML documents and document several bytes. The client browser reads the HTML response data, formatted according to its HTML syntax, and displayed in the browser window.

For example: Type the URL in the browser address bar, then press Enter to go through the following process:

1, the browser requests the URL to the DNS server to resolve the domain name corresponding to the IP address;

2, the IP address parsed, based on the established default IP address and port 80, and the server TCP connection ;

3, the browser issues a read file (URL domain name corresponding to the rear portion of the file) of the HTTP request, the request packet as a TCP three-way handshake third data packets to the server;

4, the server responds to the browser request, and sends the html text corresponding to the browser;

5, releasing the TCP connection ;

6, the browser displays the html and text content;  

The difference between GET and POST requests

GET request
GET /books/?sex=man&name=Professional HTTP/1.1
Host: www.wrox.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: Keep-Alive

Note the last line is a blank line

POST request
POST / HTTP/1.1
Host: www.wrox.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)
Gecko/20050225 Firefox/1.0.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 40
Connection: Keep-Alive

name=Professional%20Ajax&publisher=Wiley

1, GET, request the data is then either deposited URL (that is, the data is placed into the HTTP protocol header), to divide the URL and transmit data, a plurality of parameters & connect; for example:? Login.action name = hyddd & password =? idontknow & verify =% E4% BD% A0% E5% A5% BD. If the data is in English letters / numbers, as it is transmitted, if the space is converted to +, if Chinese / other characters, the string directly BASE64 encryption, obtained as:% E4% BD% A0% E5% A5% BD, where XX XX% for the symbols in the ASCII hexadecimal representation.

POST submission: submit the data packet is placed in the body of the HTTP packet. Red lettering is the actual data transmission in the above example

Therefore, the GET data submitted will be displayed in the address bar out, and POST submission, the address bar will not change

2, the size of data transmission: First statement: the HTTP protocol does not limit the size of the transfer data, HTTP protocol specification does not limit the length of the URL.

Exists in the actual development restrictions are:

The GET : has a specific browser and server URL length limit, e.g. IE URL length limit is 2083 bytes (2K + 35). For other browsers, such as Netscape, FireFox, etc., in theory, there is no length limit, that limit depends on operating system support.

So for the time GET submission, data transmission will be restricted URL length.

POST : Because not pass through the URL value, in theory, unlimited data. However, the actual requirements of each WEB server will submit data on post size limit, Apache, IIS6 has its own configuration.

3, security

GET POST of the security safe than others. For example: The GET to submit data, user name and password in clear text appears on the URL, because (1) the login page is likely to be browser cache; (2) others to view your browser history, so others can get your account number and password, in addition, use GET to submit data also may cause Cross-site request forgery attack

4, Http get, post, soap protocol is running on the http

(1) get: Request parameter is the additional sequence as a key / value pair (query string) to the URL on
the length of the query string and a web browser by a web server limits (e.g. IE supports up to 2048 characters), not suitable for transmission of large data sets at the same time, it is not safe

(2) post: request parameter in a different part of the http header transmission (named entity body), this part of the form used to transmit information, Content-type must be set: application / x-www-form- urlencoded . post designed to support the user on the web form field, which parameters are used as key / value pairs transmitted.
However: it does not support complex data types, because there is no post rules define the semantics and configuration of data transmission.

(3) soap: http post is a special version, following a special message format xml
Content-type to: text / xml xml can be of any data.

Http protocol defines a number of ways to interact with the server, there are four basic kinds, namely GET, POST, PUT, DELETE. URL address is used to describe a resource on a network, and in HTTP GET, POST, PUT, DELETE corresponding to this resource check, change, add, delete four operations. Our most common is the GET and POST. GET is generally used to get / query resource information, and POST are generally used to update resource information.

We look at the difference between GET and POST

  1. Body GET data is placed after the submitted URL to? URL and transmit the divided data, is connected to between & parameter, such as EditPosts.aspx? Name = test1 & id = 123456. POST method is the data submitted in a HTTP packet .

  2. GET submitted the data size is limited (because the browser restrictions on the length of the URL), and the data submitted by the POST method is not limited.

  3. GET requires the use Request.QueryString to get the value of a variable, while the POST method to get the value of a variable by Request.Form.

  4. Submit a GET data, it will bring security problems, such as a login page when submitting data via GET, the user name and password will appear in the URL, if the page can be cached or others can access this machine, you can from history record obtain the user ID and password.



Author: RaphetS
link: http: //www.jianshu.com/p/80e25cb1d81a
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
Published 42 original articles · won praise 57 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_36911154/article/details/78026313