IOS development network-HTTP and ios development protocols
IOS development network-HTTP protocol
Note: The apache tomcat server must use port 8080.
I. URL
1. Basic Introduction
The full name of the URL is Uniform Resource Locator (Unified Resource Locator)
You can find the only one resource on the Internet through one URL.
A URL is the address and location of a resource. Each resource on the Internet has a unique URL.
2. Common URLs
(1) HTTP
Hypertext Transfer Protocol, which accesses remote network resources in the format of http ://
Http is the most common protocol in network development.
(2) file
Access resources on the Local Computer in the format of file: // (no host address is required)
(3) mailto
The email address is accessed in the format of mailto:
(4) FTP
Access the file resources of the shared host in the format of ftp ://
Ii. HTTP protocol
1. HTTP Protocol Introduction
Whether it is a mobile client or a PC, HTTP is often used to access remote network resources.
Visit Baidu homepage: http://www.baidu.com
Get Sina Weibo data
Obtain group buying data from public comments
2. Functions of HTTP
The full name of HTTP is Hypertext Transfer Protocol, Hypertext Transfer Protocol
(1) specifying the data transmission format between the client and the server
(2) Enable the client and server to communicate data effectively
3. Why HTTP?
(1) simple and fast because the HTTP protocol is simple, the HTTP server program size is small, so the communication speed is fast
(2) Flexible HTTP allows transmission of any type of data
(3) HTTP 0.9 and 1.0 use non-persistent connection to limit that only one request can be processed for each connection. The server immediately disconnects the connection after responding to the client request. This method can save transmission time.
4. HTTP Communication Process
To use the HTTP protocol to request data from the server, you must first understand the complete process of HTTP Communication.
Complete http communication can be divided into two major steps
(1) Request: the client requests data from the server
(2) response: the server returns the corresponding data from the client.
3. HTTP Communication Process-request and response
1. HTTP Communication Process-Request
HTTP protocol: a complete HTTP request sent from a client to the server contains the following content:
Request Line: contains the request method, request resource path, and HTTP Protocol version.
GET/MJServer/resources/images/1.jpg HTTP/1.1
Request Header: contains the Environment Description of the client, the host address requested by the client, and other information.
Host: 192.168.1.105: 8080 // address of the server Host to be accessed by the client
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9) Firefox/30.0 // client type, client software environment
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
Request body: Specific data sent from the client to the server, such as file data
2. HTTP Communication Process-Response
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 code, and status name.
HTTP/1.1 200 OK
Response Header: contains the description of the server and returned data.
Server: Apache-Coyote/1.1 // Server type
Content-Type: image/jpeg // Type of the returned data
Content-Length: 56811 // The Length of the returned data
Date: Mon, 23 Jun 2014 12:54:52 GMT // Response Time
Entity content: the specific data that the server returns to the client, such as file data
3. Recommended tools firebug-1.12.5-fx.xpi
The role of the worm: intercept all http requests.
4. common response status codes
4. Methods for sending HTTP requests
1. Simple Description
Eight Methods for sending HTTP requests are defined in the http/1.1 protocol.
GET, POST, OPTIONS, HEAD, PUT, DELETE, TRACE, CONNECT, PATCH
According to the original design intention of HTTP protocol, different methods have different operations on resources.
PUT: add
DELETE: DELETE
POST: Modify
GET: Query
Tip: GET and POST are the most common ones (in fact, both GET and POST can perform addition, deletion, modification, and query)
2. get and post requests
To use GET and POST requests to interact with the server, you must first understand the concept that a parameter is the specific data transmitted to the server, such as the account and password at login.
GET and POST comparison: the main difference between GET and POST is data transmission.
GET
After the request URL? To keep up with the parameters sent to the server, multiple parameters are separated by &, such as http://ww.test.com/login? Username = 123 & pwd = 234 & type = JSON
Note: Because the browser and server have restrictions on the URL length, the parameters attached to the URL are limited. Generally, the length cannot exceed 1 kb.
POST
All parameters sent to the server are placed in the Request body.
Theoretically, there is no limit on the amount of data transmitted by POST (depending on the server's processing capability)
3. Select GET and POST
Suggestions for choosing GET and POST
(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 add, modify, or delete data, POST is recommended.
4. scheme for sending HTTP requests in iOS
In iOS, common solutions for sending HTTP requests (GET and POST) include
(1) apple native (built-in)
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.
Suggestion:
To improve development efficiency, enterprises use a third-party framework.
5. Comparison of ASI and AFN Architectures
Note: AFN is based on NSURL, ASI is based on CFHTTP, and ASI has better performance.