一、继承关系、简介
NSHTTPURLResponse->NSURLResponse->NSObject
NSHTTPURLResponse用得比较多,代表一个响应,反映着一个HTTP URL请求、存储其他特定协议的信息,如response headers
等等。在iOS里发送的HTTP请求,很多时候返回的对象都是NSHTTPURLResponse类的一个实例。
以下内容均定义在NSURLResponse.h文件
二、一些方法与属性
1. NSURLResponse
方法 | 注释 |
---|---|
- (instancetype)initWithURL:(NSURL *)URL MIMEType:(nullable NSString *)MIMEType expectedContentLength:(NSInteger)length textEncodingName:(nullable NSString *)name NS_DESIGNATED_INITIALIZER; |
指定构造方法,MIMEType:response内容的MIMEType,length :相关数据内容的期望长度,name:相关数据内容的文本编码名称 |
@property (nullable, readonly, copy) NSURL *URL; |
response的URL |
@property (nullable, readonly, copy) NSString *MIMEType; |
response的MIMEType |
@property (readonly) long long expectedContentLength; |
内容的预期长度 |
@property (nullable, readonly, copy) NSString *textEncodingName; |
内容的文本编码名称 |
@property (nullable, readonly, copy) NSString *suggestedFilename; |
服务器指定、建议的文件名 |
2. NSHTTPURLResponse
方法 | 注释 |
---|---|
- (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)); |
构造方法,url:响应生成的URL,statusCode:HTTP状态码,HTTPVersion:服务器使用的HTTP协议版本,通常是HTTP/1.1,headerFields:一个包含响应头信息的字典 |
@property (readonly) NSInteger statusCode; |
返回response的状态码 |
@property (readonly, copy) NSDictionary *allHeaderFields; |
返回响应头信息字段的字典集合 |
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode; |
返回一个本地化字符串表示响应状态码 |