1.iOS發(fā)送HTTP請(qǐng)求常用技術(shù)點(diǎn)
在iOS中讹开,常見的發(fā)送HTTP請(qǐng)求的方案有
蘋果原生自帶
NSURLConnection
:用法簡(jiǎn)單棒旗,最古老最經(jīng)典最直接的一種方案【坑比較多】
NSURLSession
:功能比NSURLConnection更加強(qiáng)大睡榆,蘋果目前比較推薦使用這種技術(shù)【2013推出狸涌,iOS7開始出的技術(shù)】
CFNetwork
:NSURL*的底層利术,純C語言第三方框架
ASIHttpRequest
:外號(hào)“HTTP終結(jié)者”敷矫,功能極其強(qiáng)大例获,可惜早已停止更新
AFNetworking
:簡(jiǎn)單易用,提供了基本夠用的常用功能曹仗,維護(hù)和使用者多
MKNetworkKit
:簡(jiǎn)單易用榨汤,產(chǎn)自三哥的故鄉(xiāng)印度,維護(hù)和使用者少
為了提高開發(fā)效率怎茫,企業(yè)開發(fā)用的基本是第三方框架收壕,兼有對(duì)蘋果原生自帶技術(shù)的封裝
2.NSURLConnection
NSURL
:請(qǐng)求地址
NSURLRequest
:一個(gè)NSURLRequest對(duì)象就代表一個(gè)請(qǐng)求,它包含的信息有
一個(gè)NSURL對(duì)象
請(qǐng)求方法轨蛤、請(qǐng)求頭蜜宪、請(qǐng)求體
請(qǐng)求超時(shí)
… …
NSMutableURLRequest
:NSURLRequest的子類
NSURLConnection
: 負(fù)責(zé)發(fā)送請(qǐng)求,建立客戶端和服務(wù)器的連接;發(fā)送數(shù)據(jù)給服務(wù)器祥山,并收集來自服務(wù)器的響應(yīng)數(shù)據(jù)
使用NSURLConnection發(fā)送請(qǐng)求的步驟:
1>創(chuàng)建一個(gè)NSURL對(duì)象圃验,設(shè)置請(qǐng)求路徑
2>傳入NSURL創(chuàng)建一個(gè)NSURLRequest對(duì)象,設(shè)置請(qǐng)求頭和請(qǐng)求體
3>使用NSURLConnection發(fā)送請(qǐng)求
蘋果SDK提供的相關(guān)方法:
NSURLConnection常見的發(fā)送請(qǐng)求方法有以下幾種
//同步請(qǐng)求
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
//異步請(qǐng)求:根據(jù)對(duì)服務(wù)器返回?cái)?shù)據(jù)的處理方式的不同缝呕,又可以分為2種
//block回調(diào)
+ (void)sendAsynchronousRequest:(NSURLRequest*)request
queue:(NSOperationQueue*)queue
completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError))handler;
//代理
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;
+ (NSURLConnection*)connectionWithRequest:(NSURLRequest *)request
delegate:(id)delegate;
- (id)initWithRequest:(NSURLRequest *)request
delegate:(id)delegate
startImmediately:(BOOL)startImmediately;
//在startImmediately = NO 的情況下澳窑,需要調(diào)用start方法開始發(fā)送請(qǐng)求
- (void)start;
- (void)cancel;//對(duì)應(yīng)終止發(fā)送請(qǐng)求
//成為NSURLConnection的代理,最好遵守 <NSURLConnectionDataDelegate> 協(xié)議
// NSURLConnectionDataDelegate協(xié)議中的代理方法
//開始接收到服務(wù)器的響應(yīng)時(shí)調(diào)用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
//接收到服務(wù)器返回的數(shù)據(jù)時(shí)調(diào)用(服務(wù)器返回的數(shù)據(jù)比較大時(shí)會(huì)調(diào)用多次)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
//服務(wù)器返回的數(shù)據(jù)完全接收完畢后調(diào)用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
//請(qǐng)求出錯(cuò)時(shí)調(diào)用(比如請(qǐng)求超時(shí))
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
3.NSURLConnection使用
3.1發(fā)送同步請(qǐng)求
/**
* 發(fā)送同步請(qǐng)求
*/
- (void)sync{
// 0.請(qǐng)求路徑
NSURL *url = [NSURL URLWithString:@"http://www.reibang.com/users/468d1f0192cb/latest_articles"];
// 1.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.發(fā)送請(qǐng)求
// sendSynchronousRequest阻塞式的方法供常,等待服務(wù)器返回?cái)?shù)據(jù)
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
// 3.解析服務(wù)器返回的數(shù)據(jù)(解析成字符串)
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@ %@", string, response.allHeaderFields);
}
3.2發(fā)送異步請(qǐng)求
/**
* 發(fā)送異步請(qǐng)求
*/
- (void)async{
NSURL *url = [NSURL URLWithString:@"http://www.reibang.com/users/468d1f0192cb/latest_articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 請(qǐng)求完畢會(huì)來到這個(gè)block
// 解析服務(wù)器返回的數(shù)據(jù)(解析成字符串)
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
NSHTTPURLResponse *r = (NSHTTPURLResponse *)response;
NSLog(@"%zd %@", r.statusCode, r.allHeaderFields);
}];
}
3.3代理方式 <NSURLConnectionDataDelegate>
@interface ViewController () <NSURLConnectionDataDelegate>
/** 用來存放服務(wù)器返回的數(shù)據(jù) */
@property (nonatomic, strong) NSMutableData *responseData;
@end
@implementation ViewController
/**
* NSURLConnection 代理使用方式
*/
- (void)delegateAysnc{
NSURL *url = [NSURL URLWithString:@"http://www.reibang.com/users/468d1f0192cb/latest_articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
}
/**
* 接收到服務(wù)器的響應(yīng)
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"didReceiveResponse");
self.responseData = [NSMutableData data];
}
/**
* 接收到服務(wù)器的數(shù)據(jù)(如果數(shù)據(jù)量比較大摊聋,這個(gè)方法會(huì)被調(diào)用多次)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// 不斷拼接服務(wù)器返回的數(shù)據(jù)
[self.responseData appendData:data];
NSLog(@"didReceiveData -- %zd", data.length);
}
/**
* 服務(wù)器的數(shù)據(jù)成功接收完畢
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"connectionDidFinishLoading");
NSString *string = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@", string);
self.responseData = nil;
}
/**
* 請(qǐng)求失敗(比如請(qǐng)求超時(shí))
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"didFailWithError -- %@", error);
}
// didReceiveResponse
// didReceiveData -- 3752
// didReceiveData -- 12131
// didReceiveData -- 3052
// connectionDidFinishLoading
// 打印出的網(wǎng)頁代碼
// #pragma mark - <NSURLConnectionDataDelegate> -- end
@end
NSURL *url = [NSURL URLWithString:@"http://www.reibang.com/users/468d1f0192cb/latest_articles"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 1.自動(dòng)發(fā)送請(qǐng)求
[NSURLConnection connectionWithRequest:request delegate:self];
// 2.自動(dòng)發(fā)送請(qǐng)求
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// 3.1 手動(dòng)發(fā)送請(qǐng)求
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn start];
// 3.2 取消發(fā)送請(qǐng)求
[conn cancel];
3.4異步線程執(zhí)行耗時(shí)操作,主線程更新UI
NSString *urlStr = @"http://www.reibang.com/users/468d1f0192cb/latest_articles";
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// 回到主線程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 把網(wǎng)頁顯示在webView上
}];
}];
3.5GET請(qǐng)求多值參數(shù)
NSURL *url = [NSURL URLWithString:@"http://www.example.com:8080/weather?place=Beijing&place=Shanghai"];
//{"weathers":[{"city":"Beijing","status":"晴轉(zhuǎn)多云"},{"city":"Shanghai","status":"晴轉(zhuǎn)多云"}]}