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;
//init方法进行对象的创建
- (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, // 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

//设置请求的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 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;
//通过字典设置HTTP请求头的键值数据
@property (nullable, copy) NSDictionary<NSString *, NSString *> *allHTTPHeaderFields;
//设置http请求头中的字段值
- (void)setValue:(nullable NSString *)value forHTTPHeaderField:(NSString *)field;
//向http请求头中添加1个字段
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
//设置http请求体 用于POST请求
@property (nullable, copy) NSData *HTTPBody;
//设置http请求体的输入流
@property (nullable, retain) NSInputStream *HTTPBodyStream;
//设置发送请求时是否发送cookie数据
@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:

//请求的URL地址
@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;
对于HTTP请求,请求回执会被封装为NSHTTPURLResponse对象,其中除了有上面那些属性外,还有如下的扩展属性:

//请求的状态码
@property (readonly) NSInteger statusCode;
//请求头中所有的字段
@property (readonly, copy) NSDictionary *allHeaderFields;


Related articles: