因为iOS的版本的不断更新,一些老方法逐渐被废弃。比如说NSURLConnection这个类,这个类的新替换类为NSURLSession。下面我将用代码进行展示:
NSString *string = @"你自己的字符串";
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
//request 默认为GET,GET方法没有参数
[request setHTTPMethod:@"POST"];
//上传参数
NSString *bodyDataString = @"自己的参数";
NSData *data = [bodyDataString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}];
//开始请求
[task resume];
网友评论