[WebApi series] Introduction to the application of HTTP in WebApi development and webapi
[01] Application of HTTP in WebApi Development
[02] about the WebApi Architecture
[03] explains how to transmit parameters through WebApi
[04] describes WebApi testing and PostMan
[05] talking about WebApi Cores
[06] detailed explanation of WebApi Exception Handling
[07] Use WebAPI to write an EF-based CURD
[08] WebAPI Identity Authentication
[09] Detailed serialization and model binding
[10] talking about how WebApi works effectively with Mvc
[11] talking about API Reference
[12] functions of interfaces in Software Architecture
[13] comparison between WebApi and WebService
[14] talking about how to design a good interface
I. Overview
HTTP is an indispensable part in Web development. in the net mvc series, we didn't explain HTTP, not because of ASP. net mvc has little to do with http, but I personally think that the combination of http and WebApi can be explained,
The effect may be better. Therefore, we will take "http details" as the opening article of the [WebApi series] for the moment.
1. What is http?
Http is a HyperText Transfer Protocol (HyperText Transfer Protocol) based on the application layer)
2. What does this chapter mainly explain?
The purpose of this chapter is to explain: When we enter:
(1). domain name resolution
(2) establish a connection over TCP (three-way handshake)
(3). TCP communication (data transmission)
(4). Disconnect (four waves)
Based on the above process, we will divide it as follows:
(1) HTTP Request
(2) HTTP Response
So let's take a look.
The HTTP protocol defines how the browser requests the World Wide Web document to the World Wide Web server and how the server sends the document to the browser. The general workflow is shown in.
1. a TCP connection requires three processes: establishing a connection (three handshakes), transmitting data, and releasing the connection (four waves );
2. http is based on the TCP/IP protocol and the application layer protocol in the layer-5 model;
3. client request URL, such as the http://www.googl.com.hk;
4. The server will respond accordingly based on client requests );
Below, we will briefly list some of the key technologies involved:
(1) based on TCP/IP
In the early days, there were roughly four-layer model structures and seven-layer model structures. After the evolution, the four-layer model structure and the seven-layer model structure were finally divided into five-layer structure models.
1. Five-layer structure model: application layer, transmission layer, network layer, data link layer and physical layer;
2. Transmission Protocol: Based on TCP/IP
3. the http protocol is based on the application layer;
4. In the five-layer structure, the client is transmitted from top to bottom, and the server is accepted from bottom to top;
5. in the five-layer structure, the protocols of each layer must be the same, at least similar (generally, when we analyze the layer, it will be horizontally abstracted, shield other layers );
6. the application layer supports multiple transmission protocols, such as http, smtp, and ftp;
7. differentiate several concepts: TCP/IP protocol, TCP protocol and UDP protocol
(1) TCP/IP Protocol: TCP/IP is a group of protocols, including TCP and IP Protocol, UDP Protocol, and ICMP Protocol) protocol and other protocol groups;
(2) TCP protocol: transmission control protocol in the transport layer;
(3) UDP Protocol: the datagram query protocol in the transport layer;
(2) Features
Http protocol has many outstanding features, but simply lists some of its features.
1. simple and fast: the http protocol is simple. When a client sends a request to the server, it only needs to send the request method and path. The transmitted content is simple and lightweight, reducing the transmission bandwidth and speed;
2. b/S mode: B/S mode (Browse/Server mode), also called the client (Google, firefox, ie)/Server mode. In Web development, it is basically based on the B/S mode;
3. No connection: the http protocol itself is connectionless. Although http uses a tcp connection, both parties do not need to establish an http connection before exchanging http insulation;
4. stateless: stateless means that the Protocol has no memory for transaction processing, that is, when the same customer accesses the page on the same server for the second time, the response of the server is the same as that of the first access;
(3) three handshakes
Each TCP connection requires three phases: establishing a connection, transmitting data, and releasing a connection. The three-way handshake occurs when the connection is established. The general process is shown in.
1. First handshake: the client requests a connection. The client sends a request connection to the server (the client sends the sys = j package to the server), enters the send Request status (syn_sent status), and waits for the server to confirm.
2. Second handshake: the Server accepts the request and sends a confirmation message to the client. When the server receives the syn packet, first confirm the customer's syn (ack = j + 1) and release a syn Packet (syn = k), that is, the syn + ack packet, the server enters the syn_recv status.
3. The third handshake: establish a connection. After receiving the syn + ack packet from the server, the client sends a brown check packet ack (ack = k + 1) to the server. After the packet is sent, the client and the server enter the connection status (connection successful) and complete three handshakes;
The above seems abstract. Let's give an example of a call:
The purpose of our call is to transmit information. Before making A call, we need to call the phone number of the other party and then the other Party responds, the following is an example of a two-person phone call:
A: Call B's mobile phone (Dialing, waiting for B to answer the phone after dialing, equivalent to the first handshake, request waiting status );
B: I saw A call from A (it was received by A and confirmed it), answered the call, and said to A: Hello, old A (it indicates to A that he is B, which is equivalent to A second handshake );
A: Hello, old B (third handshake );
Both parties greet each other. After the table name has its own identity, the call can be made (three handshakes have ended ).
(4) Four Waves
The TCP connection is full-duplex, so each direction must be closed separately. When one party completes its data sending task, it can send a FIN to terminate the connection in this direction. Receiving a FIN only means that the other party has no data flowing up, a TCP connection can still send data after receiving a FIN. First, the party that closes the service will take the initiative to close the service, and the other party will passively close the service. 1. The TCP client sends a FIN to disable data transmission from the client to the server. 2. When the server receives the FIN, it sends back an ACK and confirms that the serial number is added to 1. Like SYN, a FIN occupies a sequence number. 3. The server closes the client connection and sends a FIN to the client. 4. The client sends back the ACK message for confirmation and sets the serial number to receive the serial number plus 1.
(5) URI, URL, and URN
1. URI, URL, and URN Definitions
URI (Uniform Resource Identifier) indicates the Uniform Resource Identifier, which identifies the Resource string;
The URL (Uniform Resource Locator) represents the unified Resource Locator and the address of standard resources on the Internet;
URN (Uniform Resources Name) indicates the Name of the unified resource, the Name of the resource on the Internet;
2. parsing between URI, URL, and URN
(1) From the naming perspective, the URI identifies the resource and is unique. The URL identifies the resource address and the URN identifies the Resource Name;
(2) mathematical relationship: URI = URL + URN + URL ∩ URN; it is easy to see that the URL must be a URI, but the URI is not necessarily a URL. Similarly, the URN must be a URI, but URI is not necessarily URN;
3. the URL should have characteristics
(1) domain names are easy to remember and spell;
(2) brief;
(3) Easy to input;
(4) The site structure can be reflected;
(5) it should be "cracked". You can remove the end of the URL to reach a higher level of information architecture;
(6) persistent and unchangeable
4. Summary
Generally, URI indicates the Identifier of a Resource (Uniform Resource Identifier ). URI is a string that identifies a resource. Technically, all URLs are Uris. W3C considers "URL is an informal concept, but it is very useful:
A URL is a type of URI. It identifies a resource by representing its main access mechanism. "In other words, a URI is the identifier of a resource, the URL provides specific information for obtaining the resource.
Note: A resource is an abstract concept. It can refer to either a file or a method call result or some other content on the server.
(6) several key concepts of HTTP
This is a relatively basic concept. It should be known to all the friends who have learned computing networks. I will not explain it here. The rough outline is shown below.
2. HTTP
(1) http requests
1. http requests are generally composed of three parts: <request line> <Request Header> <Request body>
(1) Request Line: <Method> <Request-url> <version> (2) Request header: header (3) Request Body: Request-Body
Get http://localhost:2172/api/Default/GetUserInfo?UserName=Alan_beijing HTTP/1.1
host:localhost
Content-Type:application/json
2. http Request Method
2.1Get
(1) generally, the Select Operation corresponding to the database is a common method for http requests. (2) this operation is secure for the system, because it only queries and does not modify data;
(3) The results of multiple requests to the same URL should be the same; (4) parameters should be passed in the URL, rather than in the Rquest-Body;
(5) There is a length limit, such as IE: 2803; Firefox: 65536; Chrome: 8182; Safari: 80000; Opera: 190000 (6) is not safe because the parameter is exposed in the url.
2.2.Post
(1) It is generally used to Update data to the system, which corresponds to the Update operation in the database. It is a common method for http requests. (2) The parameter is transmitted in Requet-Body;
(3) safer than Get
2.3.Put
(1) It is generally used to Insert data into the system (of course, its function Post can also be implemented, and there are many similarities with Post), corresponding to the Insert operation in the DB;
(2) transfer content in Request-Body;
(3) This method is generally not used because it is insecure and does not contain a verification mechanism;
2.4.Delete
(1) It is generally used to Delete data from the system, corresponding to the Delete operation in the database;
(2) It is not safe because no verification mechanism is provided;
2.5.Head
(1) Same as Get, but Head only returns Http-Responce header information;
(2) because the Head only returns the header information (relative to Get, lightweight), it is generally used to confirm the validity of the URI and the date and time of resource update;
2.6.Patch
(1) Some document changes;
2.7.Trace
(1) Tracking path, for example, tracing the proxy passing through a resource request; (2) echo the request received by the server, mainly used for testing or diagnosis;
2.8.Copy
(1) copy the specified resource to the target location;
2.9.Options
(1) queries the method supported by the URI. (2) queries the server performance.
2.10.Link
(1) Establish a connection relationship;
2.11.UnLink
(1) disconnection
2.12.Purge
(1) clear;
2.13.PropFind
(1) view attributes
2.14.View
(1) view
2.15.Connect
(1) Tunneling Protocol connection proxy
Pai.move
(1) Move server resources to the target location;
2.17.Wrapped
(1) Allow the client to send encapsulated requests;
2.18.Extension-Method
(1) Other methods can be added on the premise of the same Protocol;
2.19.Lock
(1) Locking;
2.20.UnLock
(1) unlock;
3. Request Header
Common http request header fields are listed as follows.
(2) http Response
1. http response is generally composed of three parts: <status line> <Response Header> <response body>;
2. Status line
2.1 Status line Composition
A status line consists of three parts: <protocol and its version> <status code> <description>, for example, HTTP/1.1 200 OK;
2.2 Status Code and common status code
The status code consists of three digits, indicating the response status.
3. Response Header
The principle of the response header is similar to that of the request header.
Copyright 3
- Thank you for your reading. If you have any shortcomings, please kindly advise and learn and make progress together.
- Blog website: http://www.cnblogs.com/wangjiming /.
- A small number of articles are integrated by reading, referencing, referencing, copying, copying, and pasting. Most of them are original articles.
- If you like, please recommend it; if you have new ideas, welcome to raise, mailbox: 2098469527@qq.com.
- This blog can be reposted, but it must be a famous blog source.