About the URL Loading System

About the URL Loading System

原文链接

This guide describes the Foundation framework classes available for interacting with URLs and communicating with servers using standard Internet protocols. Together these classes are referred to as the URL loading system.

本指南描述了Foundation框架下可用于跟URL互动并且通过标准的网络协议与服务器交流的一些类。综合这些类就构成了URL加载系统。

The URL loading system is a set of classes and protocols that allow your app to access content referenced by a URL. At the heart of this technology is the NSURL class, which lets your app manipulate URLs and the resources they refer to.

URL加载系统是一系列的允许你的应用访问URL内容类和协议。这项技术的核心就是NSURL类,NSURL类使你的app能够操作URL和URL所涉及的资源。

To support that class, the Foundation framework provides a rich collection of classes that let you load the contents of a URL, upload data to servers, manage cookie storage, control response caching, handle credential storage and authentication in app-specific ways, and write custom protocol extensions.

为了支持URL类,Foundation框架提供了一个非常强大的类合集,以使你能够读取URL的内容,上传数据到服务器,cookie存储,管理响应缓存,以自己的方式处理证书存储和验证,还有自定义一些协议扩展。

The URL loading system provides support for accessing resources using the following protocols:

URL加载系统支持以下获取资源的协议:

  • File Transfer Protocol (ftp://)
  • Hypertext Transfer Protocol (http://)
  • Hypertext Transfer Protocol with encryption (https://)
  • Local file URLs (file:///)
  • Data URLs (data://)

It also transparently supports both proxy servers and SOCKS gateways using the user’s system preferences.

显然,它也支持代理服务器和用户系统设置的SOCKS网关(?)

At a Glance 概述

The URL loading system includes classes that load URLs along with a number of important helper classes that work with those URL loading classes to modify their behavior. The major helper classes fall into five categories: protocol support, authentication and credentials, cookie storage, configuration management, and cache management.

URL加载系统包含很多加载URL的类和其他的一些很重要的修饰辅助类,加载类和辅助类共同作用。主要的辅助类有五种:协议支持,证书认证,cookie存储,配置管理和缓存管理。


主类和辅助类

URL Loading

The most commonly used classes in the URL loading system allow your app to retrieve the content of a URL from the source. You can retrieve that content using NSURLSession. The specific methods you use depend largely on whether you wish to fetch data to memory or download it to disk.

URL加载系统中最常用的类,app通过这些类可以从数据源上检索URL的内容。可用NSURLSession类检索URL内容。具体用法很大程度上取决于你仅仅想读取数据到内存还是写到磁盘上。

Fetching Content as Data (In Memory) 读取到内存中

At a high level, there are two basic approaches to fetching URL data:

  • For simple requests, use the NSURLSession API to retrieve the contents from an NSURL object directly, either as an NSData object or as a file on disk.

  • For more complex requests—requests that upload data, for example—provide an NSURLRequest object (or its mutable subclass, NSMutableURLRequest) to NSURLSession.

通常有两种URL数据读取办法:

  • 简单请求,使用NSURLSession接口直接检索一个NSURL对象,得到一个NSData对象或者磁盘上的文件作为返回。
  • 高级的上传数据的用法,比如给NSURLSession传入一个NSURLRequset对象(或者可变子类,NSMutableURLRequest*)。

Regardless of which approach you choose, your app can obtain the response data in two ways:

  • Provide a completion handler block. The URL loading class calls that block when it finishes receiving data from the server.
  • Provide a custom delegate. The URL loading class periodically calls your delegate methods as it receives the data from the originating source. Your app is responsible for accumulating that data, if needed.

In addition to the data itself, the URL loading class provides your delegate or completion handler block with a response object that encapsulates metadata associated with the request, such as the MIME type and content length.

不管你选择那种办法,app能通过以下两种方式获得返回数据:

  • 完成回调的block.URL加载类会在从服务器上获取数据后调用这个block.
  • 自定义代理,URLJ加载类一收到服务器的返回数据就会周期性的调用代理方法,如果有需要的话,app就会多次收到返回数据。

除了data本身,URL加载类还会提供给的你代理或者完成回调block一个封装好这次请求相关数据(比如数据长度和MIME)的响应对象。

相关章节:Using NSURLSession

Downloading Content as a File 下载

At a high level, there are two basic approaches to downloading the contents of a URL to a file:

  • For simple requests, use the NSURLSession API to retrieve the contents from an NSURL object directly, either as an NSData object or as a file on disk.
  • For more complex requests—requests that upload data, for example—provide an NSURLRequest object (or its mutable subclass, NSMutableURLRequest) to NSURLSession.
Note: Downloads initiated by an NSURLSession instance are not cached. If you need to cache the results, your app must use either NSURLSession and write the data to disk itself.

通常有两种下载的办法:

  • 简单请求,使用NSURLSession接口直接检索一个NSURL对象,得到一个NSData对象或者磁盘上的文件作为返回。
  • 高级的上传数据的用法,比如给NSURLSession传入一个NSURLRequset对象(或者可变子类,NSMutableURLRequest*)。

跟上面一样

Note:用NSURLSession下载是没有缓存的。如果需要缓存下载结果,你必须自己写文件到磁盘和NSURLSession配合使用。

Helper Classes

The URL loading classes use two helper classes that provide additional metadata—one for the request itself (NSURLRequest) and one for the server’s response (NSURLResponse).

URL加载类有两个提供额外数据的辅助类:本地请求(NSURLRequest)和服务返回(NSURLResponse

URL Requests

An NSURLRequest object encapsulates a URL and any protocol-specific properties, in a protocol-independent manner.

Note: When a client app initiates a connection or download using an instance of NSMutableURLRequest, a deep copy is made of the request. Changes made to the initiating request have no effect after a download is initialized.

Some protocols support protocol-specific properties. For example, the HTTP protocol adds methods to NSURLRequest that return the HTTP request body, headers, and transfer method. It also adds methods to NSMutableURLRequest to set those values.

The details of working with URL request objects are described throughout this book.

一个NSURLRequest对象封装了1个URL以及相应的协议信息。

Note:当客户端app使用NSMutableURLRequest向服务器发起连接或者下载时,request会进行深拷贝,在下载初始化完成后修改request不会影响之前的设置.

一些协议支持设置其特有的属性.比如HTTP协议,在NSURLRequest中添加了返回HTTP请求体、请求头以及转换方法.同样NSMutableURLRequest也加入了这些方法。

具体怎样使用URL请求对象的办法贯穿在整篇指南中。

Redirection and Other Request Changes

Some protocols, such as HTTP, provide a way for a server to tell your app that content has moved to a different URL. The URL loading classes can notify their delegates when this happens. If your app provides an appropriate delegate method, your app can then decide whether to follow the redirect, return the response body from the redirect, or return an error.

一些协议,比如HTTP,当目标URL迁移时会通过某种方式通知客户端.当这种情况发生时,URL加载类会通知其代理.如果你的app实现了对应的代理方法,你可以选择重新请求定向后的地址还是返回一个错误信息.

Authentication and Credentials

Some servers restrict access to certain content, requiring a user to authenticate by providing some sort of credentials—a client certificate, a user name and password, and so on—in order to gain access. In the case of a web server, restricted content is grouped into a realm that requires a single set of credentials. Credentials (certificates, specifically) are also used to determine trust in the other direction—to evaluate whether your app should trust the server.

The URL loading system provides classes that model credentials and protected areas as well as providing secure credential persistence. Your app can specify that these credentials persist for a single request, for the duration of an app’s launch, or permanently in the user’s keychain.

一些服务器限制某些权限,要求通过一些证书、用户名密码等等来获取权限。在web服务器方面,限制内容被分组到一个集合,需要单独的一套认证。证书也可以被用来确认是否信任所访问的服务器.

Note: Credentials stored in persistent storage are kept in the user’s keychain and shared among all apps.
Note: 证书会一直在用户钥匙串中存储,并且所有的app共用。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容