一直在努力
分类:
2010-09-10 15:16:36
NSURLRequest
andNSMutableURLRequest
classes to provide accessors for those properties. As an example, the HTTP protocol adds methods to NSURLRequest
to return the HTTP request body, headers, and transfer method. It also adds methods toNSMutableURLRequest
to set the corresponding values. Methods for setting and getting property values in those accessors are exposed in the NSURLProtocol
class.NSHTTPURLResponse
stores the headers and the status code returned by the web server.The NSURLConnection
and NSURLDownload
classes provide the interface to make a connection specified by anNSURLRequest
object and download the contents. An NSURLConnection
object provides data to the delegate as it is received from the originating source, whereas an NSURLDownload
object writes the request data directly to disk. Both classes provide extensive delegate support for responding to redirects, authentication challenges, and error conditions.
the NSURLConnection
class provides a delegate method that allows an application to control the caching of a response on a per-request basis. Downloads initiated by an NSURLDownload
instance are not cached。
http
and https
requests are cached, and https
requests are never cached to disk.NSURLConnection
can control whether a response is cached and whether the response should be cached only in memory by implementing the connection:willCacheResponse:
delegate method.The URL loading system design allows a client application to extend the protocols that are supported for transferring data. The URL loading system natively supports http
, https
, file
, and ftp
protocols.
Custom protocols are implemented by subclassing NSURLProtocol
and then registering the new class with the URL loading system using the NSURLProtocol
class method registerClass:
. When an NSURLConnection
or NSURLDownload object initiates a connection for an NSURLRequest, the URL loading system consults each of the registered classes in the reverse order of their registration. The first class that returns YES
for a canInitWithRequest:
message is used to handle the request.
The URL loading system is responsible for creating and releasing NSURLProtocol
instances when connections start and complete. An application should never create an instance of NSURLProtocol
directly.
When an NSURLProtocol
subclass is initialized by the URL loading system, it is provided a client object that conforms to the NSURLProtocolClient protocol. The NSURLProtocol
subclass sends messages from the NSURLProtocolClient protocol to the client object to inform the URL loading system of its actions as it creates a response, receives data, redirects to a new URL, requires authentication, and completes the load. If the custom protocol supports authentication, then it must conform to the NSURLAuthenticationChallengeSender protocol.
connection:didReceiveResponse:
,connection:didReceiveData:
, connection:didFailWithError:
and connectionDidFinishLoading:
.The delegate can allow the redirect by simply returning the provided request.
The delegate can create a new request, pointing to a different URL, and return that request.
The delegate can reject the redirect and receive any existing data from the connection by returning nil
.
The delegate can cancel both the redirect and the connection by sending the cancel
message
NSURLRequestUseProtocolCachePolicy
,NSURLRequestReloadIgnoringCacheData
, NSURLRequestReturnCacheDataElseLoad
, orNSURLRequestReturnCacheDataDontLoad
.NSURLRequestReturnCacheDataDontLoad 允许app确定是否要返回cache数据,如果使用这种协议当本地不存在response的时候创建NSURLConnection or NSURLDownload实例时将会马上返回nil;这类似于离线模式,没有建立网络连接;
注:目前,只有响应HTTP和HTTPS请求被缓存起来。 FTP和FILE协议尝试访问原始地址是被允许的在cache 协议中。自定义NSURLProtocol类可以提供缓存,如果他们选择。
使用缓存协议为HTTP语义
最复杂的高速缓存的使用情况是,当一个请求使用HTTP协议,并已设置缓存协议为NSURLRequestUseProtocolCachePolicy。
如果
当前请求的NSCachedURLResponse不存在,那么就从原始地址重新获取。如果有一个缓存的响应该请求时,URL加载系统检查该response,以确定它是否表明应对其包含的内容必须重新生效。如果内容必须重新验证建立连接到源代码,看看原始资源是否已经改变。如果没有改变,然后返回响应从本地缓存。如果有改变,提取数据从原始资源处。