错误示例:
//1.创建一个网络路径
NSURL *url = [NSURL URLWithString:urlString];
// 2.创建一个网络请求
NSURLRequest *request =[NSURLRequest requestWithURL:url];
// 3.获得会话对象
NSURLSession *session = [NSURLSession sharedSession];
// 4.根据会话对象,创建一个Task任务
NSURLSessionDataTask *sessionDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
NSLog(@"从服务器获取到数据 %ld",(long)httpResponse.statusCode);
/*对从服务器获取到的数据data进行相应的处理:*/
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
NSLog(@"%@",dict);
}];
// 5.最后一步,执行任务(resume也是继续执行):
[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];;```