(Original) URLRequest details

Source: Internet
Author: User

Fannic original (reproduced please indicate the source of http://ylucy.cnblogs.com)

Use instance of URLRequest

 //  Set URL     
Nsurl * URL = [Nsurl urlwithstring: @" Http://www.stackoverflow.com " ];

// Set the request
// The constructed nsurlrequest hasA specific policy that depends on the cache response. cachepolicy gets the policy and timeoutinterval gets the timeout value.
Nsurlrequest * Request = [Nsurlrequest requestwithurl: URL
Cachepolicy: nsurlrequestreturncachedataelseload timeoutinterval: 60 ];

// Use this request and delegate to create a connection.  
_ Connection = [[Nsurlconnection alloc] initwithrequest: Request Delegate : Self];

Where:

The default Cache Policy for nsurlrequest is Nsurlrequestuseprotocolcachepolicy is the most consistent protocol. Nsurlrequestreloadignoringcachedata ignores the cache and downloads it directly from the original address. Nsurlrequestreturncachedataelseload is downloaded from the original address only when no data exists in the cache. Nsurlrequestreturncachedatadontload allows the app to determine whether to return cache data. If this protocol is used when no response exists locally, nsurlconnection or nsurldownload instance is created, it will return nil immediately. This is similar to offline mode, no network connection is established;

You only need to implement the following delegate method to process data response

-(Void) connection :( nsurlconnection *) connection didreceiveresponse :( nshttpurlresponse *) Response

-(Void) connection :( nsurlconnection *) connection didreceivedata :( nsdata *) data

-(Void) connection :( nsurlconnection *) connection didfailwitherror :( nserror *) Error

-(Void) connectiondidfinishloading :( nsurlconnection *) Connection

Nsurlconnect also provides a convenient class method: sendsynchronousrequest: returningresponse: Error: availableSynchronous locationLoad a URL request

+ (Nsdata *) sendsynchronousrequest: (nsurlrequest *) Request
Returningresponse: (nsurlresponse **) response error: (nserror
**) Error

    • The URL request to be loaded. this request object, as part of the initialization process, is deeply copied (deep-copied ). after this method is returned, modifying the request will not affect the Request Used in the loading process.
    • The reponse output parameter, which is returned by the URL returned by the server.
    • Error output parameter. If an error occurs during request processing, it is null if there is no error.

TIPS:

Shows the URL loading system class hierarchy:

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.