Http:url:
Full name is uniform Resource Locator (Uniform Resource Locator)
With 1 URLs, you can find only one resource on the Internet
Each resource has a unique URL
URL format:
Basic format: Protocol header://Host address/path
Protocol header: Different protocols, which represent different resource lookup and transmission methods. such as HTTP,AFP, etc.
Host address: The IP address (domain name) where the resource host resides
Path: The specific location of each resource at the host address
HTTP protocol:
The full name of HTTP is Hypertext Transfer Protocol, Hypertext Transfer Protocol
Specifies the format of data transfer between the client and server
Enables client and server to communicate data
HTTP protocol is the most extensive protocol in network programming
Features of the HTTP protocol
1, fast and simple: because the HTTP protocol is simple, so the HTTP server program size is small, fast communication speed
2, Flexible: HTTP allows the transfer of any type of data
3, HTTP 0.9 and 1.0 use a non-persistent connection: limit each connection to only one request, the server immediately after the client's request to disconnect, this way can save transmission time
4, HTTP 1.1 uses persistent connection: You do not have to create a new connection for each Web object, and one connection can transfer multiple objects
HTTP Communication Process Request
HTTP protocol: A completed client to the server HTTP strange request, to include the following content
Request Line: Specify request method, request resource path, and HTTP protocol version #表示访问根目录 # get/http/1.1
Request Header: Information about the client's environment, the host address requested by the client, etc.
The request header contains at least: #客户端要访问的服务器主机的地址 # host:m.baidu.com
The request header can also contain the following information:
#客户端的类型, client Software Environment # User-agent:iphone AppleWebKit
#客户端能接受的数据类型 # accept:text/html
#客户端的语言环境 #accept-language:zh-cn
#客户端支持的数据压缩格式 # Accept-encoding:gzip
#访问结束后, whether the connection is disconnected # Connection:close
Request Body (optional): Specific data that the client sends to the server, such as the file data to be uploaded
Format: At the end of each request message, use \ r \ n, after the last request, add \ r \ n (that is, the last request message is \r\n\r\n);
Response
The HTTP protocol stipulates that a full HTTP response should contain the following content
Status line: Contains the HTTP protocol version, status code, status English name
#请求成功 http/1.1 OK Status Code 200 starts with a successful
Response header: Contains a description of the server, a description of the returned data
#服务器类型 Server ...
#返回数据的类型 Contect-type:
#返回数据的长度 Contect-length:
#反应时间 Date ...
#ETag (hash value): ETA.G: "xxxxxxx";
Entity content: The specific binary data that the server returns to the client
* The status line and the response header are for the developer to debug, the entity content is for the user to see
Development scenarios
Apple officially offers three options
1, the introduction of nsurlconnection:ios2.0, the use of simple, the oldest and most direct one of the program
2, Nsurlsecssion:ios7 launch, more powerful function than nsurlconnection
3, the bottom of cfnetwork:nsurl, pure C language, almost no
Third-party frameworks
ASIHTTPRequest: Cfnetwork nickname HTTP Terminator for the underlying, extremely powerful, 2012 stop update, based on MRC
Afnetworking: The bottom layer for the nsurlconnection&nsurlsecssion simple and easy to use, to achieve basic functions, maintenance and use of many people
Mknetworkkit: Low-level for nsurlconnection easy to use, maintenance and usage of less people
Alamofire: The bottom layer for nsurlsession based on Swift lightweight development, features far less than afnetworking
Nsurlconnection steps
1. Nsurl: Determine which resources to access
2, Nsurlrequest: According to the URL resume request, to the server request data
3, Nsurlconnertion: Establish a network connection, say the request (asynchronous) sent to the server
eg
1. Create the resources you want to access
Nsurl *url=[nsurl urlwithstring:@ "http://m.baidu.com"];
2. Requesting data from the server
Nsurlrequest *request=[nsurlrequest Requestwithurl:url];
3. Establish a network connection
[Nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] completionHandler:^ ( Nsurlresponse *response,nsdata *data, Nserror *connectionerror) {
NSLog (@ "%@", data);
}];
Nsurlconnection Summary:
Sendasynchronousrequest: This method is asynchronous suffocation, and the programmer does not have to consider whether to thread or create the queue, which will send the previously built request to the server asynchronously. Wait until the server response is received, and the queue is responsible for dispatching the Comloetionhandler
Queue
Scheduling Completionhandler Execution of queues
[[Nsoperationqueue alloc]init], background thread executes asynchronously
[Nsoperationqueue Mainqueue], the main thread executes asynchronously
Selection of queues:
Select New queue scheduling if you want to take time-consuming processing of data, such as decompression compression, etc.
If you simply want to handle it, update the UI now and select the Home column
Completionhandler
Network access is over, callback method after receiving response data to the server
Response, the response of the server, which is usually called when the download function is developed
Response Property Description:
URL server Feedback URL, sometimes the server will redirect the new URL
The MIMEType server tells the client that the binary data type is returned, and ContentType knows what software to use to process the returned data based on the MIMEType client.
StatusCode Status code: 1XX message 2XX Success 3XX more Select 4XX Client error 5XX Server error
Expectedcontentlength data length, download file total length
Suggestedfilename Recommended file name
The binary data returned by the data server and what the programmer cares about most
Connectionerror connection error, any network access error may occur
Request Brief
Create a request based on a URL
Set request properties: Cache policy, Network timeout market
Tell the server about additional information: client type, authentication, etc...
[Request setvalue:@ "IPhone AppleWebKit" forkey:@ "user-agent"];
Web Note 01-1