1 First, Nsurlconnection2 1. Send a request3 1>send a sync request4+ (NSData *) Sendsynchronousrequest: (nsurlrequest *) Request Returningresponse: (Nsurlresponse *) Response error: ( Nserror * *) error;5 6 2>Send an asynchronous request (block)7+ (void) Sendasynchronousrequest: (nsurlrequest*) Request8Queue: (nsoperationqueue*) Queue9Completionhandler: (void(^) (nsurlresponse* response, nsdata* data, nserror*connectionerror)) handler;Ten One 3>Send an asynchronous request (proxy method) A[Nsurlconnection Connectionwithrequest:requestDelegate: self]; -[[Nsurlconnection alloc] Initwithrequest:requestDelegate: self]; -[[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self startimmediately:yes]; the -Nsurlconnection *conn = [[Nsurlconnection alloc] Initwithrequest:requestDelegate: Self startimmediately:no]; - [conn start]; - + 2. File Download (large file download) - 1>implementation scenario: Side Download Side writes (writes to a file in the sandbox) + 2>Concrete Implementation Steps A A. When receiving a response from the server at //Create an empty file-Nsfilemanager - [Mgr CreateFileAtPath:self.destPath contents:nil Attributes:nil]; - - //creates a handle object associated with an empty file-Nsfilehandle - [Nsfilehandle FileHandleForWritingAtPath:self.destPath]; - in B. When receiving data from the server - //writes the data returned by the server to the end of the file using a handle object to //move to the end of a file + [Self.writehandle seektoendoffile]; - //writes data starting at the current moving position (end of file) the [Self.writehandle Writedata:data]; * $ c. When the data returned by the server is receivedPanax Notoginseng //Close Handle - [Self.writehandle CloseFile]; theSelf.writehandle =Nil; + A 3. Breakpoint Download the 1>Key Technology points +*set the request header range to tell the server which data to download - $ 4. File Upload $ 1>Clear -*only post requests can be used -*request parameters are in the request body (file parameters \ non-file type normal parameters) the - 2>implementation StepsWuyi A. Stitching the request body (file parameters \ non-file type of normal parameters) the*file Parameters - //start tag of the parameter (split line) Wu--nihao\r\n - //parameter Description (parameter name ...) AboutContent-disposition:form-data; Name="Name of parameter"; Filename="file name"\ r \ n $ //File Type -content-Type : Types of files mimetype\r\n - //Binary data for files (parameter values) - \ r \ n A Binary data for files + \ r \ n the -*Non-file parameters (normal parameters) $ //start tag of the parameter (split line) the--nihao\r\n the //parameter Description (parameter name ...) theContent-disposition:form-data; Name="Name of parameter"\ r \ n the //parameter Values - \ r \ n in parameter Values the \ r \ n the About*mark with end of all parameters the--nihao--\ r \ n the the B. Setting the request header +*the length of the request body -content-Length: Size of the request body (byte length) the Bayi*type of request data thecontent-Type: the //normal POST request: application/x-www-form-urlencoded - //POST request to upload file: multipart/form-data; Boundary=--nihao
iOS nsurlconnection detailed (2)