Knowledge about HTTP protocol for iOS development, and knowledge about ios
HTTP principles
HTTP request Selection
How to send an HTTP request1. What is a URL?
Before introducing HTTP, we have some knowledge about the URL, because only through the URL can we get the resources on the network. So what is a URL?
URL (Uniform Resource Locator unified Resource Locator): the URL is actually the address and location of the Resource on the Internet. Each Resource on the Internet has a unique URL, only through URLs can we find the only resource on the Internet.
Basic URL structure: Protocol: // host address/path
Http://www.cnblogs.com/iOSClub/articles/5233432.html
Http: // 192.168.38.24/imgs/01.png
Protocol: different protocols determine different resource search and transmission methods.
Host address: the IP address of the resource host (server)
Path: the specific location of the resource on the host (server)
2. Several Common URLs
After learning about what a URL is, what are common http protocols in a URL?
Note: special characters such as spaces and Chinese characters cannot appear in URLs.
- All characters in the URL must be ASCII characters;
- The URL cannot contain Chinese characters or special characters (such as spaces );
Therefore, if the URL contains Chinese characters, you need to add a percent translation.
UrlString = [urlStringstringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
3. What is HTTP?
HTTP Protocol (HyperText Tranfer Protocol): HTTP specifies the data transmission mode between the client and the server.
Principle of the underlying layer of HTTP: The underlying layer of HTTP is to establish a connection to the communication pipeline through socket to achieve data transmission. HTTP is a TCP transmission protocol and a reliable and secure protocol.
HTTP is the most commonly used protocol in network development. Whether it is a mobile client or a PC, HTTP is often used to access network resources.
4. What does HTTP do?
The role of HTTP can be summarized in one sentence: to make the client and server data transmission effective.
5. Why HTTP protocol (advantages of HTTP Protocol)
6. Introduction to the communication process of the HTPP Protocol
After learning about the HTTP protocol, we will continue to understand the HTTP communication process. The HTTP communication process includes two aspects:
HTTP request: the client requests data from the server.
HTTP protocol: a complete HTTP protocol consists of three parts: request line, request header, and request body.
- Request Line: mainly includes the request method, request path, and HTTP Protocol version.
"GET/resources/images/HTTP/1.1"
- Request Header: mainly contains the description of the client environment and host address information requested by the client.
Host: 192.168.38.24: 8080 class = "Apple-tab-span"> // address of the server Host that the client wants to access
Accept: text/html // data type that the client can receive
Accept-Language: zh-cn // Language environment of the Client
Accept-Encoding: gzip // The data compression format supported by the client
Host: m.baidu.com // address of the server Host to be accessed by the client
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv: 37.0) Gecko/20100101 Firefox/37.0 // client type, client software environment
- Request body: Specific data sent from the client to the server, such as files/Data
HTTP response: the server returns the desired data from the client.
When the client sends a request to the server, the server should respond, that is, return data to the client.
HTTP protocol: a complete HTTP response contains the following content:
- Status line: contains the HTTP protocol version, status, status code corresponding to the English Name HTTP/1.1 200 OK
- Response Header: contains the description of the server and returned data.
Content-Encoding: gzip // The data compression format supported by the server
Content-Length: 1528 // The Length of the returned data
Content-Type: application/xhtml + xml; charset = UTF-8 // Type of returned data
Date: Mon, 15 Jun 2015 09:06:46 GMT // Response Time
Server: apache // Server type
- Object content: the specific data that the server returns to the client (image/html/file ...)
7. Selection of HTTP requests 1. Brief description of HTTP requests
1. HTTP Method
HTTP defines many methods that correspond to different resource operations. The most common methods are GET and POST.
GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT, PATCH
Add: PUT
DELETE: DELETE
Change: POST
Query: GET
Because GET and POST can implement all the above operations, in actual development, we only need to use the GET and POST methods.
2. Parameters
When interacting with the server, you sometimes need to send some data to the server. For example, you need to send a user name and password during login.
Parameter: refers to the specific data transmitted to the server.
2. GET request
GET: in essence, data is obtained from the server, which is more efficient. And GET requests can be cached.
Note: The network cache data is stored in the SQLite database (Path: NSHomeDirectory ()).
After the request URL? Parameters are spliced in the form of "parameter name" = "parameter value". Multiple parameters are separated.
Note: The length of GET is limited. Different browsers have different length limits, generally between 2 ~ Between 8 K.
3. POST request
POST: in essence, it is used to send data to the server and obtain the results processed by the server. The efficiency is not as high as that of GET. POST requests cannot be cached. After each refresh, the form must be submitted again.
All the parameters sent to the server are placed in the 'request body'. Theoretically, there is no limit on the amount of data transmitted by POST.
Note: all data related to user privacy (passwords/bank card numbers, etc.) must be transmitted using POST.
4. Select GET and POST requests
For the selection of GET and POST requests, refer to the following four principles:
1. If you want to transmit a large amount of data, such as file upload, you can only use POST requests.
2. GET is more secure than POST. If it contains confidential/sensitive information, POST is recommended.
3. If you only request data (data query), we recommend that you use GET
4. If you want to add, modify, or delete data, POST is recommended.
8. How to send an HTTP request
Suggestion: to improve development efficiency, enterprises use third-party frameworks.
1. Native sending requests from Apple
- NSURLConnection: simple to use, the oldest, the most classic, and the most direct solution
- NSURLSession: a new technology developed in iOS 7 with more powerful functions than NSURLConnection
- CFNetwork: bottom layer of NSURL *, pure C Language
2. Third-party framework
- ASIHttpRequest: the nickname "HTTP Terminator", which is extremely powerful. Unfortunately, the update has been stopped.
- AFNetworking: it is easy to use and provides basic and adequate common functions.
The previous blog has already explained how to send a request.