
學(xué)習(xí)NSURLSession之前、先擼一遍NSURLRequest(請(qǐng)求)和NSURLResponse(響應(yīng))頭文件里的屬性和API
本文鏈接
NSURLRequest
@interface NSURLRequest : NSObject <NSSecureCoding, NSCopying, NSMutableCopying>
{
@private
NSURLRequestInternal *_internal;
}
/*!
創(chuàng)建NSURLRequest對(duì)象
默認(rèn)使用NSURLRequestUseProtocolCachePolicy緩存邏輯
默認(rèn)請(qǐng)求超時(shí)時(shí)限為60s
*/
+ (instancetype)requestWithURL:(NSURL *)URL;
/*!
是否支持安全編碼
*/
+ (BOOL)supportsSecureCoding;
/*!
創(chuàng)建時(shí)設(shè)置緩存策略和超時(shí)時(shí)限
@param URL 請(qǐng)求鏈接
@param cachePolicy 緩存策略
@param timeoutInterval 超時(shí)時(shí)間
*/
+ (instancetype)requestWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
//init方法進(jìn)行對(duì)象的創(chuàng)建 默認(rèn)使用NSURLRequestUseProtocolCachePolicy緩存邏輯 默認(rèn)請(qǐng)求超時(shí)時(shí)限為60s
- (instancetype)initWithURL:(NSURL *)URL;
/*!
創(chuàng)建NSURLRequest對(duì)象
默認(rèn)使用NSURLRequestUseProtocolCachePolicy緩存邏輯
默認(rèn)請(qǐng)求超時(shí)時(shí)限為60s
*/
- (instancetype)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
/*!
只讀屬性、獲取對(duì)象的URL
*/
@property (nullable, readonly, copy) NSURL *URL;
/*!
只讀屬性、獲取對(duì)象的緩存策略
*/
/*
NSURLRequestCachePolicy枚舉如下:
typedef NS_ENUM(NSUInteger, NSURLRequestCachePolicy)
{
//默認(rèn)的緩存協(xié)議
NSURLRequestUseProtocolCachePolicy = 0,
//無論有無本地緩存數(shù)據(jù) 都進(jìn)行從新請(qǐng)求
NSURLRequestReloadIgnoringLocalCacheData = 1,
//忽略本地和遠(yuǎn)程的緩存數(shù)據(jù) 未實(shí)現(xiàn)的策略
NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4,
//無論有無緩存數(shù)據(jù) 都進(jìn)行從新請(qǐng)求
NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
//先檢查緩存 如果沒有緩存再進(jìn)行請(qǐng)求
NSURLRequestReturnCacheDataElseLoad = 2,
//類似離線模式,只讀緩存 無論有無緩存都不進(jìn)行請(qǐng)求
NSURLRequestReturnCacheDataDontLoad = 3,
//未實(shí)現(xiàn)的策略
NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};
*/
@property (readonly) NSURLRequestCachePolicy cachePolicy;
/*!
只讀屬性、獲取對(duì)象的超時(shí)時(shí)間
*/
@property (readonly) NSTimeInterval timeoutInterval;
/*!
只讀屬性、獲取緩存路徑
*/
@property (nullable, readonly, copy) NSURL *mainDocumentURL;
/*!
只讀屬性、獲取網(wǎng)絡(luò)請(qǐng)求的服務(wù)類型
*/
/*
typedef NS_ENUM(NSUInteger, NSURLRequestNetworkServiceType)
{
NSURLNetworkServiceTypeDefault = 0, // 普通網(wǎng)絡(luò)傳輸,默認(rèn)使用這個(gè)
NSURLNetworkServiceTypeVoIP = 1, // 網(wǎng)絡(luò)語音通信傳輸,只能在VoIP使用
NSURLNetworkServiceTypeVideo = 2, // 影像傳輸
NSURLNetworkServiceTypeBackground = 3, // 網(wǎng)絡(luò)后臺(tái)傳輸,優(yōu)先級(jí)不高時(shí)可使用。對(duì)用戶不需要的網(wǎng)絡(luò)操作可使用
NSURLNetworkServiceTypeVoice = 4 // 語音傳輸
};
*/
@property (readonly) NSURLRequestNetworkServiceType networkServiceType;
/*!
只讀屬性、獲取是否允許蜂窩請(qǐng)求
*/
@property (readonly) BOOL allowsCellularAccess;
@end
NSMutableURLRequest
NSURLRequest的子類、放開了許多只讀權(quán)限
@interface NSMutableURLRequest : NSURLRequest
/*!
設(shè)置請(qǐng)求的URL
*/
@property (nullable, copy) NSURL *URL;
/*!
設(shè)置請(qǐng)求的緩存策略
*/@property NSURLRequestCachePolicy cachePolicy;
/*!
設(shè)置請(qǐng)求的超時(shí)時(shí)間
*/
@property NSTimeInterval timeoutInterval;
/*!
設(shè)置請(qǐng)求的緩存目錄
*/
@property (nullable, copy) NSURL *mainDocumentURL;
/*!
設(shè)置請(qǐng)求的網(wǎng)絡(luò)服務(wù)類型
*/
@property NSURLRequestNetworkServiceType networkServiceType NS_AVAILABLE(10_7, 4_0);
/*!
設(shè)置請(qǐng)求是否支持蜂窩網(wǎng)絡(luò)
*/
@property BOOL allowsCellularAccess NS_AVAILABLE(10_8, 6_0);
NSURLRequest (NSHTTPURLRequest)
NSURLRequest的擴(kuò)展、可以針對(duì)HTTP進(jìn)行許多設(shè)置
@interface NSURLRequest (NSHTTPURLRequest)
/*!
設(shè)置HTTP請(qǐng)求的方式、默認(rèn)為Get
*/
@property (copy) NSString *HTTPMethod;
//通過字典設(shè)置HTTP請(qǐng)求頭的鍵值數(shù)據(jù)
/*!
通過字典、設(shè)置HTTP請(qǐng)求頭
*/
@property (nullable, copy) NSDictionary<NSString *, NSString *> *allHTTPHeaderFields;
/*!
通過鍵值對(duì)、設(shè)置HTTP請(qǐng)求頭
*/
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString *)field;
/*!
通過鍵值對(duì)、為HTTP請(qǐng)求頭添加字段
*/
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
/*!
在Post方式下、設(shè)置請(qǐng)求體
*/
@property (nullable, copy) NSData *HTTPBody;
/*!
設(shè)置http請(qǐng)求體的輸入流
*/
@property (nullable, retain) NSInputStream *HTTPBodyStream;
/*!
發(fā)送請(qǐng)求時(shí)是否發(fā)送cookie數(shù)據(jù)
*/
@property BOOL HTTPShouldHandleCookies;
/*!
請(qǐng)求時(shí)是否按順序收發(fā)
*/
@property BOOL HTTPShouldUsePipelining;
@end
NSURLResponse
@interface NSURLResponse : NSObject <NSSecureCoding, NSCopying>
{
@package
NSURLResponseInternal *_internal;
}
/*!
你可以自己生成一個(gè)NSURLResponse對(duì)象
@param URL 鏈接
@param MIMEType 響應(yīng)類型(`application/json`等等)
@param length 響應(yīng)內(nèi)容長度
@param name 編碼的名稱
@result The initialized NSURLResponse.
@discussion This is the designated initializer for NSURLResponse.
*/
- (instancetype)initWithURL:(NSURL *)URL MIMEType:(nullable NSString *)MIMEType expectedContentLength:(NSInteger)length textEncodingName:(nullable NSString *)name NS_DESIGNATED_INITIALIZER;
/*!
返回響應(yīng)對(duì)象的URL
*/
@property (nullable, readonly, copy) NSURL *URL;
/*!
返回響應(yīng)類型(`application/json`等等)
*/
@property (nullable, readonly, copy) NSString *MIMEType;
/*!
返回響應(yīng)內(nèi)容長度
*/
@property (readonly) long long expectedContentLength;
/*!
返回編碼類型
*/
@property (nullable, readonly, copy) NSString *textEncodingName;
/*!
返回響應(yīng)文件的文件名(比如:person_object.json)
*/
@property (nullable, readonly, copy) NSString *suggestedFilename;
@end
NSHTTPURLResponse : NSURLResponse
@class NSHTTPURLResponseInternal;
@interface NSHTTPURLResponse : NSURLResponse
{
@package
NSHTTPURLResponseInternal *_httpInternal;
}
/*!
@param url 鏈接
@param statusCode 狀態(tài)碼(404等等)
@param HTTPVersion HTTP版本(HTTP1.1/HTTP 2.0等)
@param headerFields 響應(yīng)頭(字典)
*/
- (nullable instancetype)initWithURL:(NSURL *)url statusCode:(NSInteger)statusCode HTTPVersion:(nullable NSString *)HTTPVersion headerFields:(nullable NSDictionary<NSString *, NSString *> *)headerFields API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
/*!
返回狀態(tài)碼
*/
@property (readonly) NSInteger statusCode;
/*!
返回請(qǐng)求頭字典
*/
@property (readonly, copy) NSDictionary *allHeaderFields;
/*!
將HTTP狀態(tài)碼轉(zhuǎn)換成字符串
對(duì)照表:http://www.zhimengzhe.com/IOSkaifa/264293.html
*/
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode;
@end
```
###參考資料
[NSHTTPURLResponse的localizedStringForStatusCode](http://www.zhimengzhe.com/IOSkaifa/264293.html)