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互動并且通過標準的網絡協(xié)議與服務器交流的一些類。綜合這些類就構成了URL加載系統(tǒng)。

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加載系統(tǒng)是一系列的允許你的應用訪問URL內容類和協(xié)議蜜宪。這項技術的核心就是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的內容堕虹,上傳數(shù)據(jù)到服務器卧晓,cookie存儲,管理響應緩存赴捞,以自己的方式處理證書存儲和驗證逼裆,還有自定義一些協(xié)議擴展。

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

URL加載系統(tǒng)支持以下獲取資源的協(xié)議:

  • 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.

顯然赦政,它也支持代理服務器和用戶系統(tǒng)設置的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加載系統(tǒng)包含很多加載URL的類和其他的一些很重要的修飾輔助類,加載類和輔助類共同作用。主要的輔助類有五種:協(xié)議支持桐愉,證書認證财破,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加載系統(tǒng)中最常用的類左痢,app通過這些類可以從數(shù)據(jù)源上檢索URL的內容≌登螅可用NSURLSession類檢索URL內容抖锥。具體用法很大程度上取決于你僅僅想讀取數(shù)據(jù)到內存還是寫到磁盤上亿眠。

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數(shù)據(jù)讀取辦法:

  • 簡單請求却音,使用NSURLSession接口直接檢索一個NSURL對象酷愧,得到一個NSData對象或者磁盤上的文件作為返回。
  • 高級的上傳數(shù)據(jù)的用法,比如給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能通過以下兩種方式獲得返回數(shù)據(jù):

  • 完成回調的block.URL加載類會在從服務器上獲取數(shù)據(jù)后調用這個block.
  • 自定義代理泼疑,URLJ加載類一收到服務器的返回數(shù)據(jù)就會周期性的調用代理方法,如果有需要的話岔帽,app就會多次收到返回數(shù)據(jù)玫鸟。

除了data本身,URL加載類還會提供給的你代理或者完成回調block一個封裝好這次請求相關數(shù)據(jù)(比如數(shù)據(jù)長度和MIME)的響應對象犀勒。

相關章節(jié):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對象或者磁盤上的文件作為返回贾费。
  • 高級的上傳數(shù)據(jù)的用法钦购,比如給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加載類有兩個提供額外數(shù)據(jù)的輔助類:本地請求(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以及相應的協(xié)議信息唱凯。

Note:當客戶端app使用NSMutableURLRequest向服務器發(fā)起連接或者下載時,request會進行深拷貝,在下載初始化完成后修改request不會影響之前的設置.

一些協(xié)議支持設置其特有的屬性.比如HTTP協(xié)議,在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.

一些協(xié)議,比如HTTP,當目標URL遷移時會通過某種方式通知客戶端.當這種情況發(fā)生時,URL加載類會通知其代理.如果你的app實現(xiàn)了對應的代理方法,你可以選擇重新請求定向后的地址還是返回一個錯誤信息.

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共用蝎亚。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市先馆,隨后出現(xiàn)的幾起案子发框,更是在濱河造成了極大的恐慌,老刑警劉巖煤墙,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件梅惯,死亡現(xiàn)場離奇詭異,居然都是意外死亡仿野,警方通過查閱死者的電腦和手機铣减,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來脚作,“玉大人葫哗,你說我怎么就攤上這事∏蛱危” “怎么了劣针?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長亿扁。 經常有香客問我捺典,道長,這世上最難降的妖魔是什么从祝? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任襟己,我火速辦了婚禮,結果婚禮上哄褒,老公的妹妹穿的比我還像新娘稀蟋。我一直安慰自己,他們只是感情好呐赡,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布退客。 她就那樣靜靜地躺著,像睡著了一般链嘀。 火紅的嫁衣襯著肌膚如雪萌狂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天怀泊,我揣著相機與錄音茫藏,去河邊找鬼。 笑死霹琼,一個胖子當著我的面吹牛务傲,可吹牛的內容都是我干的凉当。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼售葡,長吁一口氣:“原來是場噩夢啊……” “哼看杭!你這毒婦竟也來了?” 一聲冷哼從身側響起挟伙,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤楼雹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后尖阔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贮缅,經...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年介却,在試婚紗的時候發(fā)現(xiàn)自己被綠了谴供。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡筷笨,死狀恐怖憔鬼,靈堂內的尸體忽然破棺而出龟劲,到底是詐尸還是另有隱情胃夏,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布昌跌,位于F島的核電站仰禀,受9級特大地震影響,放射性物質發(fā)生泄漏蚕愤。R本人自食惡果不足惜答恶,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望萍诱。 院中可真熱鬧悬嗓,春花似錦、人聲如沸裕坊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽籍凝。三九已至周瞎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間饵蒂,已是汗流浹背声诸。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留退盯,地道東北人彼乌。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓泻肯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親慰照。 傳聞我的和親對象是個殘疾皇子软免,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內容