NSURLConnection:支持ftp://,http://,https://,file:///(本地文件)链接
异步链接请求,执行该函数后就当即开始链接;通常来讲都在主线程中调用该函数;若在新线程中调用,则回调函数不会被执行,需经过
scheduleInRunLoop使回调生效
+ (NSURLConnection *)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate
在其余线程中使异步链接的回调生效
通常状况下,咱们使用NSURLConnection时,都使用以下的代码:
_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self];
这段代码建立的connection运行在main runloop以及NSEventTrackingRunLoopMo
de下,而该模式在mouse-dragging loops and other sorts of user interface tracking loops期间会阻止对该connection的代理通知事件,好比在UITableView滚动的时候,connection的代理方法就不会被通知。这不是咱们想要的,若是想要在mouse-dragging loops and other sorts of user interface tracking loops期间咱们的connection的代理方法仍然被调用,可用如下代码替换上面的代码:
_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self startImmediately:NO];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_connection start];
NSURLRequest:url请求,包括一系列获取属性值方法,不能设置,要设置只能使用
NSMutableURLRequest
初始化一个请求,第一个构造函数等同于第二个构造函数的缓存策略参数
为
NSURLRequestUseProtocolCachePolicy
,超时时间60秒
+ (id)requestWithURL:(NSURL *)theURL
+ (id)requestWithURL:(NSURL *)theURL cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval
NSMutableURLRequest:
NSURLRequest子类,实际上该类就是提供了
NSURLRequest全部属性的设置方法
经常使用设置函数:
- (void)setHTTPBody:(NSData *)data
- (void)setHTTPMethod:(NSString *)method 默认是GET
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field 与
setValue
的不一样:若已存在一个键值,则附加新值到旧值的后面,以逗号分隔
NSURLResponse:
- (NSString *)suggestedFilename 返回网络数据须要保存的文件名
NSHTTPURLResponse:
NSURLResponse的子类
- (NSInteger)statusCode 获取状态码
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode 根据状态码获取本地化文本内容,好比状态码200表示无错误
NSURL: