錯(cuò)誤示例:
//1.創(chuàng)建一個(gè)網(wǎng)絡(luò)路徑
NSURL *url = [NSURL URLWithString:urlString];
// 2.創(chuàng)建一個(gè)網(wǎng)絡(luò)請(qǐng)求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.獲得會(huì)話對(duì)象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根據(jù)會(huì)話對(duì)象策肝,創(chuàng)建一個(gè)Task任務(wù)
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"從服務(wù)器獲取到數(shù)據(jù) %ld",(long)httpResponse.statusCode);
/*對(duì)從服務(wù)器獲取到的數(shù)據(jù)data進(jìn)行相應(yīng)的處理:*/
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
NSLog(@"%@",dict);
}];
// 5.最后一步啤握,執(zhí)行任務(wù)(resume也是繼續(xù)執(zhí)行):
[sessionDataTask resume];```
>正確示例
``` Nsstring*urlString=@"https://graph.facebook.com/debug_token?input_token=/*facebook return token*/&access_token=/*apptoken*/";```
###urlString=[urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];###
``` NSURL *url = [NSURL URLWithString:urlString];;```