NSURL *url = [NSURL URLWithString:@"http://oy3n0hshj.bkt.clouddn.com/shenhe/paobuji.txt"];
//2、創(chuàng)建請(qǐng)求(Request)對(duì)象(默認(rèn)為GET請(qǐng)求);
NSURLRequest *requst = [[NSURLRequest alloc]initWithURL:url];
//3偶房、發(fā)送請(qǐng)求
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象垃环,創(chuàng)建一個(gè)Task任務(wù):
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"從服務(wù)器獲取到數(shù)據(jù)");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"%@",@(httpResponse.statusCode));
/*
HTTP: Status 200 – 服務(wù)器成功返回網(wǎng)頁(yè)
HTTP: Status 404 – 請(qǐng)求的網(wǎng)頁(yè)不存在
HTTP: Status 503 – 服務(wù)不可用
*/
}];
// 5.最后一步,執(zhí)行任務(wù)(resume也是繼續(xù)執(zhí)行):
[sessionDataTask resume];