NSURLConnection(現(xiàn)在已經(jīng)不用探越,僅作為了解)
NSURL:請(qǐng)求地址
-
NSURLRequest:一個(gè)NSURLRequest對(duì)象就代表一個(gè)請(qǐng)求狡赐,包含以下內(nèi)容:
- 一個(gè)NSURL對(duì)象
- 請(qǐng)求方法、請(qǐng)求頭钦幔、請(qǐng)求體
- 請(qǐng)求超時(shí)等等
NSMutableRequest:NSURLRequest的子類
-
NSURLConnection:
- 負(fù)責(zé)發(fā)送請(qǐng)求枕屉,簡(jiǎn)歷客服端和服務(wù)器的鏈接
- 發(fā)送數(shù)據(jù)給服務(wù)器,并收集來自服務(wù)器的響應(yīng)數(shù)據(jù)
-
使用NSURLConnnection發(fā)送請(qǐng)求的步驟:
- 創(chuàng)建一個(gè)URL對(duì)象鲤氢,設(shè)置請(qǐng)求路徑
- 傳入NSURL創(chuàng)建一個(gè)NSURLRequest對(duì)象搀擂,設(shè)置請(qǐng)求頭和請(qǐng)求體
- 使用NSURLConnection發(fā)送請(qǐng)求
默認(rèn)發(fā)送的都是GET
發(fā)送同步請(qǐng)求
// 設(shè)置url
NSURL *url = [NSURL URLWithString:@"http://www.eyee.com"];
// 創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 發(fā)送請(qǐng)求
// sendSynchronousRequest阻塞式方法,需要等待服務(wù)器返回?cái)?shù)據(jù)
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// 解析服務(wù)器返回的數(shù)據(jù)(解析成字符串)
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// 獲取所有的返回頭
response.allHeaderFields;
發(fā)送異步請(qǐng)求-block
// 設(shè)置url
NSURL *url = [NSURL URLWithString:@"http://www.eyee.com"];
// 創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
/**
發(fā)送請(qǐng)求
@param request 請(qǐng)求對(duì)象
@param queue 處理block的隊(duì)列
*/
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// 請(qǐng)求完畢會(huì)來到這個(gè)block
// response:返回頭
// data:返回的數(shù)據(jù)
// connectionError:錯(cuò)誤信息
}];
發(fā)送異步請(qǐng)求-代理
- 需要遵守協(xié)議
NSURLConnectionDataDelegate
// 設(shè)置url
NSURL *url = [NSURL URLWithString:@"http://www.eyee.com"];
// 創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 創(chuàng)建連接對(duì)象
// 創(chuàng)建完畢后卷玉,自動(dòng)發(fā)送異步請(qǐng)求
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// 創(chuàng)建完畢后哨颂,自動(dòng)發(fā)送異步請(qǐng)求
[NSURLConnection connectionWithRequest:request delegate:self];
// startImmediately == YES 自動(dòng)發(fā)送異步請(qǐng)求
// startImmediately == NO 需要手動(dòng)發(fā)送
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
// 發(fā)送請(qǐng)求
[conn start];
// 取消請(qǐng)求
[conn cancel];
// 下面是代理方法
/**
接收到服務(wù)端響應(yīng)
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
}
/**
接收到服務(wù)器的數(shù)據(jù)
如果數(shù)據(jù)量大,這個(gè)方法會(huì)被調(diào)用多次
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
/**
服務(wù)器數(shù)據(jù)接收完畢
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
/**
請(qǐng)求失敗
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}
POST請(qǐng)求
// 設(shè)置url
NSURL *url = [NSURL URLWithString:@"http://www.eyee.com"];
// 創(chuàng)建請(qǐng)求對(duì)象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 更改請(qǐng)求方法
request.HTTPMethod = @"POST";
// 設(shè)置請(qǐng)求提
request.HTTPBody = [@"username=123&pwd=123" dataUsingEncoding:NSUTF8StringEncoding];
// 設(shè)置超時(shí)
request.timeoutInterval = 5;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if(connectionError){
// 有錯(cuò)誤
}else{
// 沒有錯(cuò)誤
}
}];
請(qǐng)求地址中有中文
stringByAddingPercentEscapesUsingEncoding
// 將中文URL進(jìn)行轉(zhuǎn)碼
NSString *urlStr = @"http://www.eyee.com/login2?username=大牛&pwd=123"
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];