1、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,
// 網絡語音通信傳輸,只能在VoIP使用
NSURLNetworkServiceTypeVoIP = 1,
// 影像傳輸
NSURLNetworkServiceTypeVideo = 2,
// 網絡后臺傳輸,優先級不高時可使用。對用戶不需要的網絡操作可使用
NSURLNetworkServiceTypeBackground = 3,
// 語音傳輸
NSURLNetworkServiceTypeVoice = 4
};
*/
@property (readonly) NSURLRequestNetworkServiceType networkServiceType;
/*!
只讀屬性、獲取是否允許蜂窩請求
*/
@property (readonly) BOOL allowsCellularAccess;
@end
2、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);
3、NSURLRequest (NSHTTPURLRequest)
NSURLRequest的擴展,基本都是只讀屬性。
@interface NSURLRequest (NSHTTPURLRequest)
/*!
獲取HTTP請求的方式
*/
@property (nullable, readonly, copy) NSString *HTTPMethod;
/*!
獲取所有的請求頭
*/
@property (nullable, readonly, copy) NSDictionary<NSString *, NSString *> *allHTTPHeaderFields;
/*!
獲取HTTP請求頭的值
*/
- (nullable NSString *)valueForHTTPHeaderField:(NSString *)field;
/*!
獲取Post方式下的請求體
*/
@property (nullable, readonly, copy) NSData *HTTPBody;
/*!
獲取http請求體的輸入流
*/
@property (nullable, readonly, retain) NSInputStream *HTTPBodyStream;
/*!
發送請求時是否發送cookie數據
*/
@property (readonly) BOOL HTTPShouldHandleCookies;
/*!
請求時是否按順序收發
*/
@property (readonly) BOOL HTTPShouldUsePipelining API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0));
@end
4、NSMutableURLRequest (NSMutableHTTPURLRequest)
NSMutableURLRequest的擴展,放開了許多只讀權限,可以針對HTTP進行許多設置。
@interface NSMutableURLRequest (NSMutableHTTPURLRequest)
/*!
設置HTTP請求的方式、默認為Get
*/
@property (copy) NSString *HTTPMethod;
/*!
通過字典、設置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 API_AVAILABLE(macos(10.7), ios(4.0), watchos(2.0), tvos(9.0));
@end
原文鏈接: