Request Class Nsurlrequest
Summary of common methods and properties in the Nsurlrequest class:
To create a default request object from a class method
/*
The request object created in this manner defaults to the Nsurlrequestuseprotocolcachepolicy cache logic default request time-out period of 60s
*/
+ (Instancetype) Requestwithurl: (Nsurl *) URL;
Returns a bool value that is used to determine whether a secure encoding is supported
+ (BOOL) supportssecurecoding;
Set the caching logic and time-out period when the initialization method of the request object is created
+ (Instancetype) Requestwithurl: (Nsurl *) URL CachePolicy: (nsurlrequestcachepolicy) CachePolicy TimeoutInterval: ( Nstimeinterval) timeOutInterval;
Init method to create object by default use Nsurlrequestuseprotocolcachepolicy cache logic default request Timeout time is 60s
-(Instancetype) Initwithurl: (Nsurl *) URL;
Init method to create an object
-(Instancetype) Initwithurl: (Nsurl *) URL CachePolicy: (nsurlrequestcachepolicy) CachePolicy timeOutInterval: ( Nstimeinterval) timeOutInterval;
Read-only property gets the URL of the request object
@property (Nullable, ReadOnly, copy) Nsurl *url;
Read-only property cache policy enumeration
/*
The Nsurlrequestcachepolicy enumeration is as follows:
typedef ns_enum (Nsuinteger, Nsurlrequestcachepolicy)
{
Default caching protocol
Nsurlrequestuseprotocolcachepolicy = 0,
A new request, regardless of whether there is any local cache data
Nsurlrequestreloadignoringlocalcachedata = 1,
Ignore policies that are not implemented by local and remote cached data
Nsurlrequestreloadignoringlocalandremotecachedata = 4,
A new request, regardless of any cached data
Nsurlrequestreloadignoringcachedata = Nsurlrequestreloadignoringlocalcachedata,
Check the cache before requesting it if it is not cached
Nsurlrequestreturncachedataelseload = 2,
Similar to offline mode, read-only caching is not requested with or without caching
Nsurlrequestreturncachedatadontload = 3,
Policies that are not implemented
Nsurlrequestreloadrevalidatingcachedata = 5,//unimplemented
};
*/
@property (readonly) Nsurlrequestcachepolicy CachePolicy;
Read-only property gets the timeout time for the request
@property (readonly) Nstimeinterval timeOutInterval;
Main document address this address is used to store the cache
@property (Nullable, ReadOnly, copy) Nsurl *maindocumenturl;
The service type that gets the network request is enumerated as follows
/*
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;
Get whether to allow service provider cellular network
@property (readonly) BOOL allowscellularaccess;
The nsurlrequest request class can set some properties in addition to initialization, and most of the properties are read-only and cannot be set and modified. Another class Nsmutableurlrequest can more flexibly set the related properties of the request.
Summary of common methods and attributes in 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 the property settings of the Request object and the HTTP/HTTPS protocol related requests:
The following properties must be set using the Nsmutableurlrequest class and, if they are nsurlrequest, can only be read and cannot be 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请求头中添加一个字段
- (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;
Request Receipt Class Nsurlresponse Properties Introduction
Nsurlresponse class to hold the requested receipt information, when sending a network request, if the request succeeds, first receives the service end receipt information, starts to receive the concrete return data directly. The main Nsurlresponse objects are the following properties:
//请求的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;