Summary of iOS's HTTP request and request acknowledgement class usage

  • 2020-06-07 05:20:53
  • OfStack

Request class NSURLRequest
Summary of common methods and properties in NSURLRequest classes:

通过这种方式创建的请求对象 默认使用NSURLRequestUseProtocolCachePolicy缓存逻辑 默认请求超时时限为60s
+ (instancetype)requestWithURL:(NSURL *)URL;
//返回1个BOOL值 用于判断是否支持安全编码
+ (BOOL)supportsSecureCoding;
//请求对象的初始化方法 创建时设置缓存逻辑和超时时限
+ (instancetype)requestWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
//init方法进行对象的创建 默认使用NSURLRequestUseProtocolCachePolicy缓存逻辑 默认请求超时时限为60s
- (instancetype)initWithURL:(NSURL *)URL;
- (instancetype)initWithURL:(NSURL *)URL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
//只读属性 获取请求对象的URL
@property (nullable, readonly, copy) NSURL *URL;
//只读属性 缓存策略枚举
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, // Standard internet traffic
    NSURLNetworkServiceTypeVoIP = 1, // Voice over IP control traffic
    NSURLNetworkServiceTypeVideo = 2, // Video traffic
    NSURLNetworkServiceTypeBackground = 3, // Background traffic
    NSURLNetworkServiceTypeVoice = 4    // Voice data
@property (readonly) NSURLRequestNetworkServiceType networkServiceType;
@property (readonly) BOOL allowsCellularAccess;
The NSURLRequest request class can only set 1 property when it is initialized, but after it is created, most properties are read-only and cannot be set or modified. The other class, NSMutableURLRequest, allows more flexibility in setting the attributes associated with requests.

Summary of common methods and properties in the NSMutableURLRequest class

@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 Request Object Property Settings for HTTP/HTTPS protocol-related requests:

The following properties must be set using the NSMutableURLRequest class. If it is NSURLRequest, it can only be read and not modified.

//设置HPPT请求方式 默认为“GET”
@property (copy) NSString *HTTPMethod;
@property (nullable, copy) NSDictionary<NSString *, NSString *> *allHTTPHeaderFields;
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString *)field;
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
//设置http请求体 用于POST请求
@property (nullable, copy) NSData *HTTPBody;
@property (nullable, retain) NSInputStream *HTTPBodyStream;
@property BOOL HTTPShouldHandleCookies;
//设置请求时是否按顺序收发 默认禁用 在某些服务器中设为YES可以提高网络性能
@property BOOL HTTPShouldUsePipelining;

An introduction to the NSURLResponse property of the request return class
The NSURLResponse class stores the return receipt information of the request. When sending the network request, if the request is successful, the server will first receive the return information and directly begin to receive the specific return data. The NSURLResponse object has the following main attributes:

@property (nullable, readonly, copy) NSURL *URL;
@property (nullable, readonly, copy) NSString *MIMEType;
@property (readonly) long long expectedContentLength;
@property (nullable, readonly, copy) NSString *textEncodingName;
//返回拼接的数据文件名 以url为名 数据没醒MIMEType为扩展名
@property (nullable, readonly, copy) NSString *suggestedFilename;

@property (readonly) NSInteger statusCode;
@property (readonly, copy) NSDictionary *allHeaderFields;

Related articles: