HTTP principle
- What is a URL
- Several common protocols in URLs
- What is the HTTP protocol
- What does HTTP do?
- Why to use the HTTP protocol
- Introduction to the communication process of HTPP protocol
- HTTP request
- HTTP response
- Selection of HTTP requests
- Comparison of two methods of sending requests (scenario)
- GET request
- POST request
- How to send an HTTP request
- How to send an apple native
- Through a third-party framework
1. What is a URL
Before we introduce HTTP, we have a certain understanding of the URL, because only through the URL we can get the resources on the network. So what exactly is a URL?
URL (Uniform Resource Locator Uniform Resource Locator): The URL is actually the resources on the Internet address, location, every resource on the Internet has a unique URL, only through the URL we can find the only resources on the Internet.
The basic composition of the URL: 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 ways to find and transmit resources
Host address: The IP address that holds the resource host (server)
Path: The location of the resource on the host (server)
Several common protocols in 2.URL
Knowing what a URL is, what are the HTTP protocols commonly used in URLs?
- HTTP//Hypertext Transfer Protocol, the way to access remote network resources, is also our most commonly used protocol
- ftp://How to access the resources of a shared host
- file://How to access local computer resources
- mailto://How to access your e-mail address
Note: spaces and special symbols such as Chinese are not allowed in URLs.
- In the URL, all characters must be ASCII code;
- No Chinese and special symbols (such as spaces) can appear in the URL;
Therefore, if Chinese is present in the URL, you need to add a percent translation.
Urlstring=[urlstringstringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
3. What is the HTTP protocol
HTTP protocol (hypertext Tranfer Protocol Hypertext Transfer Protocol): HTTP specifies how data is transferred between the client and the server.
http Underlying principle : the bottom of the HTTP is through the socket to establish a connection to the communication pipeline, to achieve data transmission. HTTP is a TCP Transport protocol (mode) that is a reliable and secure protocol.
The HTTP protocol is the most commonly used protocol in network development. Whether it is a mobile client or a PC, accessing network resources often uses the HTTP protocol.
What does 4.HTTP do?
The role of HTTP can be summarized in a nutshell: The efficient transfer of data from the client and the server.
5. Why to use the HTTP protocol (the advantages of the HTTP protocol)
- Flexible: The HTTP protocol allows the transmission of various types of data.
- Simple and fast: The HTTP protocol is simple and transmits fast. Because the protocol is simple and the HTTP server is small, the communication speed is fast.
- The HTTP protocol is a short connection (non-persistent connection): HTTP limits each time a connection is processed only one request, the server disconnects immediately after responding to the request, saving transmission time. (this is used after HTTP 0.9/1.0)
6. Introduction to the communication process of the HTPP protocol
After understanding the introduction of the HTTP protocol, continue to understand the HTTP protocol communication process, HTTP communication process contains two aspects:
HTTP request: Client wants data on server side
The HTTP protocol stipulates that a complete HTTP protocol consists of three parts: a request line, a request header, a request body
- Request line: Main include request method, request path, HTTP protocol version
"get/resources/images/http/1.1"
- Request Header: Mainly contains the description of the client environment, the host address information requested by the client.
host:192.168.38.24:8080 class= "Apple-tab-span" >//the server host address that the client wants to access
The type of data that the accept:text/html//client can receive
ACCEPT-LANGUAGE:ZH-CN//Client language environment
Accept-encoding:gzip//client-supported data compression formats
host:m.baidu.com//The server host address that the client wants to access
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 that the client sends to the server, such as File/data
HTTP response: The server returns the data the client wants
The client sends a request to the server and the server should respond by returning the data to the client
The HTTP protocol specifies that a complete HTTP response contains the following:
- Status line: Contains the HTTP protocol version, status, status code corresponding to the English name http/1.1
- Response header: Contains a description of the server and a description of the returned data.
Content-encoding:gzip//server supported data compression format
content-length:1528//length of returned data
Content-type:application/xhtml+xml;charset=utf-8//type of return data
Date:mon, June 09:06:46 GMT//Response time
Server:apache//server type
- Entity content: Specific data returned by the server to the client (image/html/file ...)
7. Selection of HTTP requests 1. A simple description of the HTTP request
Method of 1.HTTP
The HTTP protocol defines a number of methods that correspond to different resource operations, the most common of which are the get and POST methods.
GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT, PATCH
Add: PUT
Delete: Delete
Change: POST
Check: GET
Because get and post can do all of the above, so in real-world development, we just use the Get and post methods to do it.
2. Parameters
When interacting with a server, it is sometimes necessary to send some data to the server, such as the need to send a user name and password when logging in.
Parameter: Refers to the specific data that is passed to the server.
2. Get request
Get: The essence is to get the data from the server, more efficient. And get requests can be cached.
Note : The network cache data is stored in the SQLite database (path: Nshomedirectory ()).
Behind the request URL? In the form of the parameters issued to the server, parameters are "parameter name" = "argument value" in the form of splicing, multiple parameters separated by &.
Note: The length of get is limited, and different browsers have different length limits, generally between 2~8k .
3. Post request
POST: The essence is to send data to the server, you can also get the result after the server processing, efficiency is not as good as Get.post request can not be cached, after each refresh will need to resubmit the form.
The parameters sent to the server are all placed in the ' request body '; In theory, there is no limit to the amount of data delivered by post.
Note: All data related to user's privacy (password/bank card number etc...) Must be delivered by post.
4. Selection of Get and post requests
The selection of Get and post requests can be referred to the following four principles:
1. If you want to pass large amounts of data, such as file uploads, you can only use POST requests
2.GET security is worse than post, if it contains confidential \ Sensitive information, it is recommended to use post
3. If you only request data (data query), we recommend the use of Get
4. If you are adding, modifying, or deleting data, it is recommended to use post
8. How to send an HTTP request
Recommendation: In order to improve development efficiency, enterprise development is basically a third-party framework
1. Apple Native Send Request
- Nsurlconnection: Simple to use, the oldest and most direct of a program
- Nsurlsession:ios 7 new technology, features more powerful than nsurlconnection
- Cfnetwork:nsurl* 's bottom, pure C language
2. Third-party frameworks
- ASIHTTPRequest: Nickname "http Terminator", the function is extremely powerful, unfortunately has already stopped the update
- Afnetworking: Simple and easy to use, providing a basic enough common function
Specific how to send the request, the previous blog has to do not repeat the instructions here.
iOS Development HTTP protocol Related knowledge Summary