iOS基礎深入補完計劃--NSURLRequest/NSURLResponse相關API

學習NSURLSession之前、先擼一遍NSURLRequest(請求)和NSURLResponse(響應)頭文件里的屬性和API

本文鏈接


NSURLRequest

@interface NSURLRequest : NSObject <NSSecureCoding, NSCopying, NSMutableCopying>
{
    @private
    NSURLRequestInternal *_internal;
}

/*! 
創建NSURLRequest對象
默認使用NSURLRequestUseProtocolCachePolicy緩存邏輯 
默認請求超時時限為60s
*/
+ (instancetype)requestWithURL:(NSURL *)URL;
/*! 
是否支持安全編碼
*/
+ (BOOL)supportsSecureCoding;
/*!
    創建時設置緩存策略和超時時限

    @param URL 請求鏈接
    @param cachePolicy 緩存策略
    @param timeoutInterval 超時時間
*/
+ (instancetype)requestWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
//init方法進行對象的創建 默認使用NSURLRequestUseProtocolCachePolicy緩存邏輯 默認請求超時時限為60s
- (instancetype)initWithURL:(NSURL *)URL;
/*! 
創建NSURLRequest對象
默認使用NSURLRequestUseProtocolCachePolicy緩存邏輯 
默認請求超時時限為60s
*/
- (instancetype)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
/*! 
    只讀屬性、獲取對象的URL
*/
@property (nullable, readonly, copy) NSURL *URL;
/*! 
    只讀屬性、獲取對象的緩存策略
*/
/*
NSURLRequestCachePolicy枚舉如下:
typedef NS_ENUM(NSUInteger, NSURLRequestCachePolicy)
{
    //默認的緩存協議
    NSURLRequestUseProtocolCachePolicy = 0,
    //無論有無本地緩存數據 都進行從新請求
    NSURLRequestReloadIgnoringLocalCacheData = 1,
    //忽略本地和遠程的緩存數據 未實現的策略
    NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, 
    //無論有無緩存數據 都進行從新請求
    NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,
    //先檢查緩存 如果沒有緩存再進行請求
    NSURLRequestReturnCacheDataElseLoad = 2,
    //類似離線模式,只讀緩存 無論有無緩存都不進行請求
    NSURLRequestReturnCacheDataDontLoad = 3,
    //未實現的策略
    NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};
*/
@property (readonly) NSURLRequestCachePolicy cachePolicy;
/*! 
    只讀屬性、獲取對象的超時時間
*/
@property (readonly) NSTimeInterval timeoutInterval;
/*! 
    只讀屬性、獲取緩存路徑
*/
@property (nullable, readonly, copy) NSURL *mainDocumentURL;
/*! 
    只讀屬性、獲取網絡請求的服務類型
*/
/*
typedef NS_ENUM(NSUInteger, NSURLRequestNetworkServiceType)
{
    NSURLNetworkServiceTypeDefault = 0, // 普通網絡傳輸,默認使用這個
    NSURLNetworkServiceTypeVoIP = 1,    // 網絡語音通信傳輸,只能在VoIP使用
    NSURLNetworkServiceTypeVideo = 2,   // 影像傳輸
    NSURLNetworkServiceTypeBackground = 3, // 網絡后臺傳輸,優先級不高時可使用。對用戶不需要的網絡操作可使用
    NSURLNetworkServiceTypeVoice = 4       // 語音傳輸
};
*/
@property (readonly) NSURLRequestNetworkServiceType networkServiceType;
/*! 
    只讀屬性、獲取是否允許蜂窩請求
*/
@property (readonly) BOOL allowsCellularAccess;

@end

NSMutableURLRequest

NSURLRequest的子類、放開了許多只讀權限

@interface NSMutableURLRequest : NSURLRequest
/*! 
    設置請求的URL
*/
@property (nullable, copy) NSURL *URL;
/*! 
    設置請求的緩存策略
*/@property NSURLRequestCachePolicy cachePolicy;
/*! 
    設置請求的超時時間
*/
@property NSTimeInterval timeoutInterval;
/*! 
    設置請求的緩存目錄
*/
@property (nullable, copy) NSURL *mainDocumentURL;
/*! 
    設置請求的網絡服務類型
*/
@property NSURLRequestNetworkServiceType networkServiceType NS_AVAILABLE(10_7, 4_0);
/*! 
    設置請求是否支持蜂窩網絡
*/
@property BOOL allowsCellularAccess NS_AVAILABLE(10_8, 6_0);

NSURLRequest (NSHTTPURLRequest)

NSURLRequest的擴展、可以針對HTTP進行許多設置

@interface NSURLRequest (NSHTTPURLRequest) 
/*! 
    設置HTTP請求的方式、默認為Get
*/
@property (copy) NSString *HTTPMethod;
//通過字典設置HTTP請求頭的鍵值數據
/*! 
    通過字典、設置HTTP請求頭
*/
@property (nullable, copy) NSDictionary<NSString *, NSString *> *allHTTPHeaderFields;
/*! 
    通過鍵值對、設置HTTP請求頭
*/
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString *)field;
/*! 
    通過鍵值對、為HTTP請求頭添加字段
*/
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
/*! 
    在Post方式下、設置請求體
*/
@property (nullable, copy) NSData *HTTPBody;
/*! 
    設置http請求體的輸入流
*/
@property (nullable, retain) NSInputStream *HTTPBodyStream;
/*! 
    發送請求時是否發送cookie數據
*/
@property BOOL HTTPShouldHandleCookies;
/*! 
    請求時是否按順序收發
*/
@property BOOL HTTPShouldUsePipelining;
@end

NSURLResponse

@interface NSURLResponse : NSObject <NSSecureCoding, NSCopying>
{
    @package
    NSURLResponseInternal *_internal;
}

/*!
    你可以自己生成一個NSURLResponse對象
    @param URL 鏈接
    @param MIMEType 響應類型(`application/json`等等)
    @param length 響應內容長度
    @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;

/*! 
    返回響應對象的URL
*/
@property (nullable, readonly, copy) NSURL *URL;

/*! 
    返回響應類型(`application/json`等等)
*/
@property (nullable, readonly, copy) NSString *MIMEType;

/*! 
    返回響應內容長度
*/
@property (readonly) long long expectedContentLength;

/*! 
    返回編碼類型
*/
@property (nullable, readonly, copy) NSString *textEncodingName;

/*!
    返回響應文件的文件名(比如:person_object.json)
*/
@property (nullable, readonly, copy) NSString *suggestedFilename;

@end

NSHTTPURLResponse : NSURLResponse

@class NSHTTPURLResponseInternal;

@interface NSHTTPURLResponse : NSURLResponse 
{
    @package
    NSHTTPURLResponseInternal *_httpInternal;
}

/*!
  @param    url 鏈接
  @param    statusCode 狀態碼(404等等)
  @param    HTTPVersion HTTP版本(HTTP1.1/HTTP 2.0等)
  @param    headerFields 響應頭(字典)
*/
- (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));

/*! 
    返回狀態碼
*/
@property (readonly) NSInteger statusCode;

/*! 
    返回請求頭字典
*/
@property (readonly, copy) NSDictionary *allHeaderFields;

/*! 
    將HTTP狀態碼轉換成字符串
    對照表:http://www.zhimengzhe.com/IOSkaifa/264293.html
*/
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode;

@end
```

###參考資料
[NSHTTPURLResponse的localizedStringForStatusCode](http://www.zhimengzhe.com/IOSkaifa/264293.html)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。