NSURLRequest提供一种快捷的方式来下载url对应的内容,提供了创建、取消接口。并且支持delegate的一系列回调方法,用于控制链接的各个方面。这些方法分为五类:URL loading, cache management, authentication and credentials, cookie storage, and protocol support.
使用NSURLRequest建立url请求,需要提供delegate中最起码的以下几个方法 connection:didReceiveResponse:, connection:didReceiveData:,connection:didFailWithError: and connectionDidFinishLoading:.
By default the data for a connection is cached according to the support provided by the NSURLProtocol subclass that handles the request. An NSURLConnection delegate can further refine that behavior by implementingconnection:willCacheResponse:.
This delegate method can examine the provided NSCachedURLResponse object and change how the response is cached, for example restricting its storage to memory only or preventing it from being cached altogether. It is also possible to insert objects in an NSCachedURLResponse’s user info dictionary, causing them to be stored in the cache as part of the response.
Note: The delegate receives connection:willCacheResponse: messages only for protocols that support caching.
The example in Listing 6 prevents the caching of https responses. It also adds the current date to the user info dictionary for responses that are cached.
Listing 6 Example connection:withCacheResponse: implementation
You can estimate the progress of an HTTP POST upload with the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: delegate method. Note that this is not an exact measurement of upload progress, because the connection may fail or the connection may encounter an authentication challenge.
Downloading Data Synchronously
NSURLConnection provides support for downloading the contents of an NSURLRequest in a synchronous manner using the class method sendSynchronousRequest:returningResponse:error:. Using this method is not recommended, because it has severe limitations:
The client application blocks until the data has been completely received, an error is encountered, or the request times out.
Minimal support is provided for requests that require authentication.
There is no means of modifying the default behavior of response caching or accepting server redirects.
If the download succeeds, the contents of the request are returned as an NSData object and an NSURLResponse for the request is returned by reference. If NSURLConnection is unable to download the URL, the method returns nil and any available NSError instance by reference in the appropriate parameter.
If the request requires authentication in order to make the connection, valid credentials must already be available in the NSURLCredentialStorage, or must be provided as part of the requested URL. If the credentials are not available or fail to authenticate, the URL loading system responds by sending the NSURLProtocol subclass handling the connection a continueWithoutCredentialForAuthenticationChallenge: message.
When a synchronous connection attempt encounters a server redirect, the redirect is always honored. Likewise the response data is stored in the cache according to the default support provided by the protocol implementation.