【译】NSURLRequest的官方文档

Overview

NSURLRequest objects represent a URL load request in a manner independent of protocol and URL scheme.

NSURLRequest encapsulates two basic data elements of a load request: the URL to load, and the policy to use when consulting the URL content cache made available by the implementation.

NSURLRequest is designed to be extended to support additional protocols by adding categories that access protocol specific values from a property object using NSURLProtocol’spropertyForKey:inRequest: and setProperty:forKey:inRequest: methods.

The mutable subclass of NSURLRequest is NSMutableURLRequest.

概述:

NSURLRequest 对象代表一个URL加载的请求,在某种意义上是独立的协议和URL方案。

NSURLRequest封装了两个基本的数据元素在一个加载请求中:一是要加载的URL,另一个是当查询URL内容缓存通过实现是否可行时采用的策略。

NSURLRequest是设计用来扩展支持附加协议的。某个属性对象使用NSURLProtocol的propertyForKey:inRequest: 和 setProperty:forKey:inRequest: 方法,可以添加各种指定的接入协议值,NSURLRequest都支持。

可变的NSURLRequest子类为NSMutableURLRequest。

Class Methods

requestWithURL:

Creates and returns a URL request for a specified URL with default cache policy and timeout value.

创建并返回一个URL请求,指向一个指定的URL,采用默认的缓存策略和超时响应时长。

+ (id)requestWithURL:(NSURL *)theURL

Discussion

The default cache policy is NSURLRequestUseProtocolCachePolicy and the default timeout interval is 60 seconds.

默认的缓存策略是NSURLRequestUseProtocolCachePolicy,默认的超时时长是60秒。

requestWithURL:cachePolicy:timeoutInterval:

Creates and returns an initialized URL request with specified values.

创建并返回一个初始化的URL请求,采用指定的缓存策略和超时时长。

+ (id)requestWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicytimeoutInterval:(NSTimeInterval)timeoutInterval

Return Value

The newly created URL request.

Instance Methods

allHTTPHeaderFields

Returns a dictionary containing all the receiver’s HTTP header fields.

返回一个包含所有接受者HTTP头区域的字典。

- (NSDictionary *)allHTTPHeaderFields

allowsCellularAccess

Returns whether the request is allowed to use the cellular radio (if present).

返回是否请求允许使用蜂窝网络。

- (BOOL)allowsCellularAccess

Return Value

YES if the cellular radio can be used; NO otherwise.

cachePolicy

Returns the receiver’s cache policy.

返回请求接受者的缓存策略。

- (NSURLRequestCachePolicy)cachePolicy

Return Value

The receiver’s cache policy.

HTTPBody

Returns the receiver’s HTTP body data.

返回接受者的HTTP数据。

- (NSData *)HTTPBody

Return Value

The receiver‘s HTTP body data.

Discussion

This data is sent as the message body of a request, as in an HTTP POST request.

这个数据作为一个请求的信息主体被发送,就像在一个HTTP里发送请求。

HTTPBodyStream

Returns the receiver’s HTTP body stream.

返回接受者的HTTP主体流。

- (NSInputStream *)HTTPBodyStream

Return Value

The receiver’s HTTP body stream, or nil if it has not been set. The returned stream is for examination only, it is not safe to manipulate the stream in any way.

接受者的HTTP主体流,如果没有被设置的话为nil。返回的流仅供检验,无论如何操作这个流是不安全的。

Discussion

The receiver will have either an HTTP body or an HTTP body stream, only one may be set for a request. A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.

接受者将会同时拥有HTTP主体和HTTP主体流,仅有一个会被设置为请求。当它复制一个NSURLRequest对象的时候,HTTP主体流被保留,但是当一个请求使用NSCoding协议到达的时候会被丢弃。

HTTPMethod

Returns the receiver’s HTTP request method.

- (NSString *)HTTPMethod

Return Value

The receiver’s HTTP request method.

Discussion

The default HTTP method is “GET”.

返回操作HTTP的方法。默认为"GET"。

HTTPShouldHandleCookies

Returns whether the default cookie handling will be used for this request.

- (BOOL)HTTPShouldHandleCookies

Return Value

YES if the default cookie handling will be used for this request, NO otherwise.

Discussion

The default is YES.

HTTPShouldUsePipelining

Returns whether the request should continue transmitting data before receiving a response from an earlier transmission.

返回在接收到前面的传输响应之前,请求是否需要持续地传输数据。

- (BOOL)HTTPShouldUsePipelining

Return Value

YES if the request should continue transmitting data; otherwise, NO.

initWithURL:

Returns a URL request for a specified URL with default cache policy and timeout value.

- (id)initWithURL:(NSURL *)theURL

Return Value

The initialized URL request.

Discussion

The default cache policy is NSURLRequestUseProtocolCachePolicy and the default timeout interval is 60 seconds.

initWithURL:cachePolicy:timeoutInterval:

Returns an initialized URL request with specified values.

- (id)initWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicytimeoutInterval:(NSTimeInterval)timeoutInterval

Discussion

This is the designated initializer for NSURLRequest.

这两个是初始化方法,不同于最开始两个的工厂方法。

mainDocumentURL

Returns the main document URL associated with the request.

- (NSURL *)mainDocumentURL

Return Value

The main document URL associated with the request.

Discussion

This URL is used for the cookie “same domain as main document” policy.

networkServiceType

Returns the network service type of the request.

返回请求的网络服务的类型。

- (NSURLRequestNetworkServiceType)networkServiceType

Return Value

The network service type of the request.

timeoutInterval

Returns the receiver’s timeout interval, in seconds.

- (NSTimeInterval)timeoutInterval

Return Value

The receiver‘s timeout interval, in seconds.

Discussion

If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out.

如果在一个连接尝试请求保持空闲了一段超过超时响应的时间,这个请求会被认为超时。

URL

Returns the request‘s URL.

- (NSURL *)URL

Return Value

The request‘s URL.

valueForHTTPHeaderField:

Returns the value of the specified HTTP header field.

- (NSString *)valueForHTTPHeaderField:(NSString *)field

Parameters

field

The name of the header field whose value is to be returned. In keeping with the HTTP RFC, HTTP header field names are case-insensitive.

返回的头区域的名称,是不区分大小写的。

Return Value

The value associated with the header field field, or nil if there is no corresponding header field.

Constants

NSURLRequestCachePolicy

These constants are used to specify interaction with the cached responses.

enum

{

NSURLRequestUseProtocolCachePolicy = 0,

NSURLRequestReloadIgnoringLocalCacheData = 1,

NSURLRequestReloadIgnoringLocalAndRemoteCacheData =4,

NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

NSURLRequestReturnCacheDataElseLoad = 2,

NSURLRequestReturnCacheDataDontLoad = 3,

NSURLRequestReloadRevalidatingCacheData = 5

};

typedef NSUInteger NSURLRequestCachePolicy;

Constants

NSURLRequestUseProtocolCachePolicy

Specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default policy for URL load requests.

在协议实现中指定缓存逻辑。是URL加载请求的默认策略。

NSURLRequestReloadIgnoringLocalCacheData

Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

指定URL加载应该加载源端数据,不应采用已存在的缓存数据来满足URL加载请求。

NSURLRequestReloadIgnoringLocalAndRemoteCacheData

Specifies that not only should the local cache data be ignored, but that proxies and other intermediates should be instructed to disregard their caches so far as the protocol allows.

指定不仅本地缓存数据不采用,并且代理和其他中介都要忽视他们的缓存,直到协议允许。

NSURLRequestReloadIgnoringCacheData

Replaced by NSURLRequestReloadIgnoringLocalCacheData.

NSURLRequestReturnCacheDataElseLoad

Specifies that the existing cached data should be used to satisfy the request, regardless of its age or expiration date. If there is no existing data in the cache corresponding the request, the data is loaded from the originating source.

指定已存的缓存数据应该用来响应请求,不管它的生命时长和过期时间。如果在缓存中没有已存数据来响应请求的话,数据从源端加载。

NSURLRequestReturnCacheDataDontLoad

Specifies that the existing cache data should be used to satisfy a request, regardless of its age or expiration date. If there is no existing data in the cache corresponding to a URL load request, no attempt is made to load the data from the originating source, and the load is considered to have failed. This constant specifies a behavior that is similar to an “offline” mode.

指定已存的缓存数据用来满足请求,不管生命时长和过期时间。如果在缓存中没有已存数据来响应URL加载请求的话,不去尝试从源段加载数据,此时认为加载请求失败。这个常量指定了一个类似于离线模式的行为。

NSURLRequestReloadRevalidatingCacheData

Specifies that the existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.

指定如果已存的缓存数据被提供它的源段确认为有效则允许使用缓存数据响应请求,否则从源段加载数据。

NSURLRequestNetworkServiceType

These constants are used to specify the network service type of a request.

enum

{

NSURLNetworkServiceTypeDefault    = 0,

NSURLNetworkServiceTypeVoIP       = 1,

NSURLNetworkServiceTypeVideo      = 2,

NSURLNetworkServiceTypeBackground = 3,

NSURLNetworkServiceTypeVoice      = 4

};

typedef NSUInteger NSURLRequestNetworkServiceType;

Constants

NSURLNetworkServiceTypeDefault

Specifies standard network traffic.

NSURLNetworkServiceTypeVoIP

Specifies that the request is for VoIP traffic.

NSURLNetworkServiceTypeVideo

Specifies that the request is for video traffic.

NSURLNetworkServiceTypeBackground

Specifies that the request is for background traffic.

NSURLNetworkServiceTypeVoice

Specifies that the request is for voice traffic.

Understanding Cache Access

The URL loading system provides a composite on-disk and in-memory cache of responses to requests. This cache allows an application to reduce its dependency on a network connection and increase its performance.

URL加载系统提供了一个磁盘和内存混合的缓存,来相应网络请求。这个缓存允许一个应用减少对网络连接的依赖,并且增加性能。

Using the Cache for a Request

An NSURLRequest instance specifies how the local cache is used by setting the cache policy to one of the NSURLRequestCachePolicy values: NSURLRequestUseProtocolCachePolicy,NSURLRequestReloadIgnoringCacheData, NSURLRequestReturnCacheDataElseLoad, orNSURLRequestReturnCacheDataDontLoad.

一个NSURLRequest实例通过设置NSURLRequestCachePolicy来指定如何使用本地缓存。

The default cache policy for an NSURLRequest instance is NSURLRequestUseProtocolCachePolicy. TheNSURLRequestUseProtocolCachePolicy behavior is protocol specific and is defined as being the best conforming policy for the protocol.

默认的缓存策略是NSURLRequestUseProtocolCachePolicy,这是为协议定义的最好的确认策略。

Setting the cache policy to NSURLRequestReloadIgnoringCacheData causes the URL loading system to load the data from the originating source, ignoring the cache completely.

设置缓存策略为NSURLRequestReloadIgnoringCacheData,会让URL加载系统从源段加载数据,完全忽略缓存。

The NSURLRequestReturnCacheDataElseLoad cache policy will cause the URL loading system to use cached data ignoring its age or expiration date, if it exists, and load the data from the originating source only if there is no cached version.

NSURLRequestReturnCacheDataElseLoad缓存策略会让URL加载系统使用缓存数据,不管其生命周期和过期时间,只有在没有缓存的时候才从源段加载数据。

The NSURLRequestReturnCacheDataDontLoad policy allows an application to specify that only data in the cache should be returned. Attempting to create an NSURLConnection or NSURLDownload instance with this cache policy returns nil immediately if the response is not in the local cache. This is similar in function to an “offline” mode and never brings up a network connection.

NSURLRequestReturnCacheDataDontLoad策略允许一个应用指定仅使用缓存中的数据。如果尝试创建一个NSURLConnection或者NSURLDownload实例,在这个缓存策略下,假如在本地缓存中没有响应,将立即返回nil。这有点类似离线模式的功能,并且从不发起一个网络连接。

Note: Currently, only responses to http and https requests are cached. The ftp and file protocols attempt to access the originating source as allowed by the cache policy. Custom NSURLProtocol classes can provide caching if they choose.

注意:最近,只有响应http和https的请求会被缓存。ftp和文件协议当被缓存策略允许的时候尝试接入源段。自定义的NSURLProtocol类能够保护缓存,如果它们被选择使用的话。

Cache Use Semantics for the http Protocol

The most complicated cache use situation is when a request uses the http protocol and has set the cache policy to NSURLRequestUseProtocolCachePolicy.

最复杂的缓存使用情况就是当一个请求使用http协议的时候,设置缓存策略为NSURLRequestUseProtocolCachePolicy。

If an NSCachedURLResponse does not exist for the request, then the data is fetched from the originating source. If there is a cached response for the request, the URL loading system checks the response to determine if it specifies that the contents must be revalidated. If the contents must be revalidated a connection is made to the originating source to see if it has changed. If it has not changed, then the response is returned from the local cache. If it has changed, the data is fetched from the originating source.

如果一个NSCachedURLResponse对于请求并不存在,数据将会从源端获取。如果请求拥有一个缓存的响应,那么URL加载系统会检查这个响应来决定,如果它指定内容必须重新生效的话。假如内容必须重新生效,将建立一个连向源端的连接来查看内容是否发生变化。假如内容没有变化,那么响应就从本地缓存返回数据。如果内容变化了,那么数据将从源端获取。

If the cached response doesn’t specify that the contents must be revalidated, the maximum age or expiration specified in the response is examined. If the cached response is recent enough, then the response is returned from the local cache. If the response is determined to be stale, the originating source is checked for newer data. If newer data is available, the data is fetched from the originating source, otherwise it is returned from the cache.

如果缓存响应没有指定内容必须重新生效,将会检查响应指定的缓存最大的生命周期和过期时间。如果缓存响应足够新,那么响应从本地缓存返回数据。如果决定的响应是旧的,那么源端就会检查是否有新的数据。如果新数据是可行的,这个数据将会从源端获取,否则将会从缓存中返回数据。

RFC 2616, Section 13 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13) specifies the semantics involved in detail.

时间: 2024-08-11 11:21:05

【译】NSURLRequest的官方文档的相关文章

Docker安全性——官方文档[译]

Docker安全性--官方文档[译] 本文译自Docker官方文档:https://docs.docker.com/articles/security/ 在审查Docker的安全时,需要考虑三个主要方面:?容器内在的安全性,由内核命名空间和cgroup中实现;?docker守护程序本身的攻击面;?加固内核安全特性,以及它们如何与容器中互动. 内核 命名空间 Kernel Namespace Docker容器中非常相似LXC容器,并且它们都具有类似的安全功能.当您以"docker run"

Unity性能优化(2)-官方文档简译

本文是Unity官方教程,性能优化系列的第二篇<Diagnosing performance problems using the Profiler window>的简单翻译. 简介 如果游戏运行缓慢,卡顿,我们知道游戏存在性能问题.在我们尝试解决问题前,需要先知道引起问题的原因.不同问题需要不同的解决方案.如果我们靠猜测或者其他项目的经验去解决问题,那么我们可能会浪费很多时间,甚至使得问题更严重. 这时我们需要性能分析,性能分析程序测量游戏运行时的各个方面性能.通过性能分析工具,我们能够透过

[译]Polymer官方文档-布局元素

原文链接: Layout elements(注:有时需翻墙或换hosts)翻译日期: 2014年8月2日翻译人员: 铁锚 译注: 点击下载本教程的示例代码: Polymer布局元素Demo相关的代码文件在 layout 目录下. 0. 准备- Chrome浏览器模拟移动设备Chrome浏览器(要求最新版,如34以上版本)模拟移动设备的方法: 打开一个标签页,按 F12,(或者在页面空白区域点鼠标右键,审查元素),打开调试窗口. 然后在将光标移动到调试窗口中, 按 ESC键,或者点击右上角的 dr

【译】StackExchange.Redis官方文档-中文版(序)

StackExchange.Redis官方文档-中文版(序) Intro 最近想深入学习一些 Redis 相关的东西.于是看了看官方的项目,发现里面有一份文档,于是打算翻译成中文,方便大家学习参考,如果有什么翻译不准确的地方,欢迎大家指出. 翻译进度 文档还有一部分还未翻译完,我会争取在三月中旬前将剩下的部分翻译结束. 翻译进度详见:https://github.com/WeihanLi/StackExchange.Redis-docs-cn 文档地址: 原文文档地址: https://gith

SolrJ API 官方文档最佳实践

以下内容译自Solr Wiki官方文档,版权没有,随意转载. Solrj 是一个访问solr的Java客户端.它提供了一个java接口用于添加.更新和查询solr索引.本页面介绍SolrJ最新版本在Solr 1.4.x及其在附带war包中的使用. 关于SolrJ在Solr1.3和Solr1.2的使用教程请移步Solrj1.3 Wiki. SolrJ/Solr版本交叉的兼容性说明 SolrJ通常保持向后兼容性,因此你可以在较老版本的Solr上使用较新版本的SolrJ,或者在较新版本的Solr上使用

HBase 官方文档0.90.4

HBase 官方文档0.90.4 Copyright ? 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision 0.90.4 配置,数据模型使用入门 Abstract 这是 Apache HBase的官方文档, Hbase是一个分布式,版本化(versioned),构建在 Apache Hadoop和 Apache ZooKeeper上的列数据库. 我(译者)熟悉Hbase的源代码,从事Hbase

别开心太早,Python 官方文档的翻译差远了

近几天,很多公众号发布了 Python 官方文档的消息.然而,一个特别奇怪的现象就发生了,让人啼笑皆非. Python 文档的中文翻译工作一直是“默默无闻”,几个月前,我还吐槽过这件事<再聊聊Python中文社区的翻译>,当时我们的进度是 10.3%,远远落后于日本和法国,甚至落后于巴西! 这次所谓的中文版,当然是未完成翻译的残品.刚查了下,整体进度是 19.7%. 翻译进度不足20% 有的公众号在发布消息的时候,说明了这不是官宣.不是正式发布版,还指出了中文版的访问地址是隐藏入口.这都是忠于

Spring Cloud官方文档中文版-服务发现:Eureka服务端

官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR3/#spring-cloud-eureka-server 文中例子我做了一些测试在:http://git.oschina.net/dreamingodd/spring-cloud-preparation Service Discovery: Eureka Server 服务发现:Eureka服务端 How to Include Eureka Server 如何创建Eurek

Android Studio官方文档之构建和运行你的APP

Android Studio官方文档之构建和运行你的APP 本文由MTJH翻译,jkYishon审校. 前言 默认情况下,Android Studio设置新的项目并且部署到模拟器或者真机设备上,只需要点击几下.使用即时运行,你并不需要构建一个新的APK即可将改变后的方法和现有的应用资源应用到一个正在运行的应用程序中,所以代码的改变是即时可见的. 点击Run来构建并运行你的APP.Android Studio通过Gradle构建你的App,选择一个部署的设备(模拟器或连接的设备),然后把你的APP