OC, oc Language

Source: Internet
Author: User
Tags tmp folder

OC, oc Language

# NSURLRequest

  • NSURLRequest encapsulates the data required for a network request, and mainly encapsulates the following information:

    • Request Path (URL)
    • Request Method (GET or POST)
    • Request Header
    • Request body
    • Timeout Parameter
  • NSURLRequest and its subclass NSMutableURLRequest

    • All the request information of the NSURLRequest is spliced after the Request Path (URL ).
    • The Request Path of NSMutableURLRequest is separated from other request information. Other request information is set through the corresponding Key.
    • NSURLRequest is usually used for GET requests.
    • NSMutableURLRequest is usually used for POST requests.
  • Procedure for NSURLRequest to encapsulate a network request

    // 1. Create the Request Path NSString * strURL = [NSString stringWithFormat: @ "(URL here)/login? Username = % @ & pwd = % @ ", @" username ", @" password "]; NSURL * url = [NSURL URLWithString:]; // 2. encapsulate the request NSURLRequest * request = [NSURLRequest requestWithURL: url] According to the request Path;
  • NSMutableURLRequest to encapsulate a network request

    // 1. create the Request Path NSURL * url = [NSURL URLWithString: @ "(URL here)/login"]; // 2. CREATE request NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url]; // 3. set the request Method request. HTTPMethod = @ "POST"; // 4. set request parameters. HTTPBody = [@ "username =" username "& pwd =" password "dataUsingEncoding: NSUTF8StringEncoding]; // 5. set timeout request. timeoutInterval = 5;

# NSURLConnection

  • Procedure for NSURLConnection to send a request

    • Create a Request Path (NSURL)
    • Encapsulate the request path into a request object (NSURLRequest) and set other Request Parameters
    • Use NSURLConnection to send synchronous/asynchronous requests
  • NSURLConnection proxy

    • NSURLConnectionDelegate

      -(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error/*** call in case of an error and request termination */
    • NSURLConnectionDataDelegate

      -(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response/*** the server response information contained in * response called when receiving the server response, it is worth comparing the total length of data in this request */-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data/*** is called when the server receives data, if the data is frequently called multiple times * usually the data returned by the server is stored in this method * You can also calculate the download progress */-(void) connectionDidFinishLoading :( NSURLConnection *) connection/*** called when data loading is complete */
    • NSURLConnectionDownloadDelegate

      -(Void) connection :( NSURLConnection *) connection didWriteData :( long) bytesWritten totalBytesWritten :( long) totalBytesWritten expectedTotalBytes :( long) expectedTotalBytes/*** this method is called every time a file is written to the sandbox */-(void) handle :( NSURLConnection *) connection totalBytesWritten :( long) totalBytesWritten expectedTotalBytes :( long) expectedTotalBytes/*** this method is the core of resumable download */-(void) connectionDidFinishDownloading :( NSURLConnection *) connection destinationURL :( NSURL *) destinationURL/*** because: the downloaded file is saved in the tmp folder, and the data in this folder will be regularly deleted by the system * This method must be implemented to change the data storage location */
  • NSURLConnection Request Method

    • Synchronous request (thread will be blocked)

      NSData * data = [NSURLConnection sendSynchronousRequest: request returningResponse: & response error: & error];/*** data: the data returned by the server, that is, the request data * request: request object * response: Server response Data * error: error message */
    • Asynchronous request

      // Method 1 (block) [NSURLConnection sendAsynchronousRequest: request queue: [[NSOperationQueue alloc] init] completionHandler: ^ (NSURLResponse * response, NSData * data, NSError * connectionError) {/*** indicates the block in which the request completes the callback. The parameter meanings are the same as those in the Tonggu request */}]; // method 2 (proxy) [NSURLConnection connectionWithRequest: request delegate: self]/*** automatically send request */NSURLConnection * connect = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: NO]; /*** manually send the request */

# Chinese processing in URLs

  • The text in the URL is to be processed, typically using UTF-8 Encoding

    // Perform the following transcoding: [urlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]

     

PS. In Xcode7, NSURLConnection has been abolished and cannot be used any more. Now NSURLSession is used instead.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.