iOS学习笔记-----URLSession的使用

时间:2025-03-29 09:01:31
#define kBoundary @"ABC" @interface ViewController () @end @implementation ViewController /* multipart/form-data 上传数据时 所需要的数据格式 HTTP请求头: .... multipart/form-data; charset=utf-8;boundary=AaB03x .... HTTP请求体: --AaB03x Content-Disposition: form-data; name="key1" value1 --AaB03x Content-Disposition: form-data; name="key2" value2 --AaB03x Content-Disposition: form-data; name="key3"; filename="file" Content-Type: application/octet-stream 图片数据... --AaB03x-- */ /** * 包装请求体 * * @param token 用户密钥 * @param text 微博正文 * @param image 上传的图片 * * @return multipart/form-data */ - (NSData *)bodyDataWithToken:(NSString *)token text:(NSString *)text image:(UIImage *)image { //key1 = @"access_token" vlaue1 = 2.00hd363CtKpsnBedca9b3f35tBYiPD //key2 = @"status" value2 = text //key3 = @"pic" value3 = image; //包装数据到请求体中 NSMutableString *mString = [[NSMutableString alloc] init]; //token [mString appendFormat:@"--%@\r\n", kBoundary]; //拼接带有双引号的字符串 需要添加\在双引号之前 [mString appendFormat:@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n"]; //拼接value1 [mString appendFormat:@"%@\r\n", token]; [mString appendFormat:@"--%@\r\n", kBoundary]; //微博正文 //key2 [mString appendFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"]; //拼接value2 [mString appendFormat:@"%@\r\n", text]; [mString appendFormat:@"--%@\r\n", kBoundary]; //图片 [mString appendFormat:@"Content-Disposition: form-data; name=\"pic\"; filename=\"file\"\r\n"]; [mString appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"]; NSLog(@"%@", mString); //将字符串 转化为NSData NSMutableData *bodyData = [[mString dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]; //拼接图片数据 //将图片转化为数据 NSData *imageData = UIImageJPEGRepresentation(image, 1); [bodyData appendData:imageData]; //结尾字符串 结束符 NSString *endString = [NSString stringWithFormat:@"\r\n--%@--\r\n", kBoundary]; NSData *endData = [endString dataUsingEncoding:NSUTF8StringEncoding]; [bodyData appendData:endData]; return [bodyData copy]; } - (IBAction)uploadImage:(id)sender { //构建URL NSURL *url = [NSURL URLWithString:@"/2/statuses/"]; //构建请求对象 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; //设置请求对象 request.URL = url; request.HTTPMethod = @"POST"; //请求头 //格式: multipart/form-data; charset=utf-8;boundary=AaB03x //拼接请求头字符串 NSString *string = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", kBoundary]; //设置请求头 [request setValue:string forHTTPHeaderField:@"Content-Type"]; //图片和文本 UIImage *ali = [UIImage imageNamed:@""]; NSString *text = @"发送微博"; NSString *token = @"2.00hd363CtKpsnBedca9b3f35tBYiP"; //创建bodyData NSData *bodyData = [self bodyDataWithToken:token text:text image:ali]; //创建会话 NSURLSession *session = [NSURLSession sharedSession]; //创建上传任务 NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:bodyData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSHTTPURLResponse *httpRes = (NSHTTPURLResponse *)response; NSLog(@"状态码:%li", httpRes.statusCode); if (error) { NSLog(@"上传出错"); } else { NSLog(@"上传成功"); } }]; //开始任务 [uploadTask resume]; }