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: