This article summarizes the methods used for HTTP communication and data upload and download in iOS development. There are third-party class libraries commonly used in network programming afnetworking or IOS7 start a new nsurlsession, and Nsurlsession's predecessor Nsurlconnection. In general, the use of afnetworking can satisfy most requirements and is more concise and efficient.
Post and get of 1.HTTP protocol
Access to network data most of the use of the HTTP protocol, the HTTP protocol to the server to request data, and then through the HTTP protocol to obtain the server response data, the common operation is mainly post and get, these two operations can meet the majority of application requirements.
The main use of a get operation scenario is to request network resources, such as access to Web pages in the browser to launch a lot of GET requests, get request the disadvantage is that the security is not enough, the parameters are in the URL, and different browsers have different length restrictions on the URL length, some complex requests do not apply. At this time, a POST request is required, all the parameters of the post request are placed in the request body, security is better, and theoretically there is no limit on the amount of data, file upload, account login, etc. are used POST request. The general GET request is only suitable for simple query requests, adding and removing changes are generally used post requests.
Some of the parameters in the 2.HTTP request
(1) Request line, including request method, request path, HTTP protocol version information
(2) The request header, which contains the host address of the server to be accessed, the cache control instruction Cache-control, the client type user-agent, the customer receives the data type accept, Client locale Accept-language and the data compression format supported by the client accept-encoding and so on
(3) If it is a POST request, there is also the request body information
Some parameters in the 3.HTTP response
(1) Status line, contains the HTTP protocol version, status code, status English name
(2) Response header, including server type servers, return data type Content-type, return data length content-length, etc.
(3) Entity content, which is the data returned by the server based on the client's request
4.Datawithcontentsofurl
This is the simplest network request, directly specify the URL to use, but the requested data will be placed in the phone, and does not apply to HTTPS requests, see previous blog "Stanford iOS7 Public Lesson 11 notes and Demo demo& access HTTPS link download data "
5.NSURLConnection
There are several ways to initiate a request, you can set the request parameters, temporarily do not make a request, in a place to trigger the request,
Here we use "-connectwithrequest:delegate:" To initiate an asynchronous request and set up a proxy to facilitate the processing of response events in subsequent agent events
The data is processed in the proxy method, and the downloaded data name gets "R Esponse" from the response information. Suggestedfilename ", consistent with the server side, it is important to note that here is a file created in the cache folder, and then constantly writing to receive data instead of using nsmutabledata because of the use of Nsmutabledata, The received data is stored in memory, which can severely affect performance if the amount of data is large. So generally do not use nsmutabledata this way.
The more complicated the situation and the continuation of the breakpoint, First, we can get the total length of the data from the server response information response.expectedcontentlength, and update the length value of the accepted data each time we receive the data, store the data in the sandbox, and re-download after the interruption, based on the length value of the currently downloaded data, set the HTTP request header "Range" in the current progress to continue to download.
6.NSURLSession
Nsurlsession is iOS7 started a new network infrastructure to replace Nsurlconnection, there are three major sub-categories: Nsurlsessiondatatask, Nsurlsessionuploadtask, Nsurlsessiondownloadtask, which are used to obtain data, upload files, and download files, respectively. and using nsurlsession can also perform upload and download tasks in the background, as well as many settings options for Nsurlsessionconfiguration, which is enough to replace or even exceed Nsurlconnection for iOS network programming.
(1) nsurlsessiondatatask
Use Datatask to get the data based on the URL
For a POST request, you can also construct a nsmutableurlrequest, set the request body, and invoke the "Datataskwithrequest" method of the session.
(2) Nsurlsessiondownloadtask
It is important to note that the download file is stored in temp folder by default, and it needs to be moved to the target location after the download is complete.
To perform a download task, you can initiate a request download directly:
It can also be handled in the delegate event:
Nsurlsession makes it easier to download breakpoints, mainly through the " Cancelbyproducingresumedata "Method callback event to get" resumedata ", resume download when downloading according to" Resumedata "
Downloadtaskwithresumedata "can.
(3) Nsurlsessionuploadtask
It is easier to use Nsurlsessionuploadtask to perform the upload task, unlike Nsurlconnection, which also needs to set many parameters in the request body, one request plus the NSData object to be uploaded, or a nsurl corresponding to the local file path.
7.AFNetworking
Afnetworking, which may be more commonly used in iOS development, makes network programming more concise.
:https://github.com/AFNetworking/AFNetworking
Two examples of usage are common:
It is important to note that:
(1) Afnetworking default is to return the data as JSON to parse, if the return data is XML or require the response data not to parse operation, directly return to NSData, you need to set the resolution type to " Afxmlparserresponseserializer ","afhttpresponseserializer".
(2) In practice, the content-type of the returned data may not be in the afnetworking supported type and need to be in the " afurlresponseserialization.m , add the corresponding type
At the same time afnetworking to Nsurlsession also carried out the encapsulation, so nsurlsession can do things, afnetworking can do, and the wording is more simple.
8. Network Status Monitoring
There are two ways to monitor network status
(1) Use "reachability" provided by Apple(non-arc, need to be aware of setting compilation parameters):
(2) Using the method provided by afnetworking:
iOS Development note 4:http network communication and network programming