1 // 2 // ViewController.m 3 // 04-UploadTask 4 // 5 #import "ViewController.h" 6 7 #define Boundary @"AaB03x" 8 9 @interface ViewController () 10 11 @end 12 13 @implementation ViewController 14 15 - (void)viewDidLoad { 16 [super viewDidLoad]; 17 // Do any additional setup after loading the view, typically from a nib. 18 } 19 20 - (void)didReceiveMemoryWarning { 21 [super didReceiveMemoryWarning]; 22 // Dispose of any resources that can be recreated. 23 } 24 /* 25 HTTP请求头: 26 .... 27 multipart/form-data; charset=utf-8;boundary=AaB03x 28 .... 29 30 HTTP请求体: 31 --AaB03x 32 Content-Disposition: form-data; name="key1" 33 34 value1 35 --AaB03x 36 Content-disposition: form-data; name="key2" 37 38 value2 39 --AaB03x 40 Content-disposition: form-data; name="key3"; filename="file" 41 Content-Type: application/octet-stream 42 43 图片数据... 44 --AaB03x-- 45 2.00hd363CtKpsnBedca9b3f35tBYiPD 46 */ 47 - (NSData *)buildBodyData:(NSData *)imgData { 48 49 NSString *accessToken = @"2.00hd363CtKpsnBedca9b3f35tBYiPD"; 50 NSString *statusValue = @"我发美女图片"; 51 52 //1.设置status 53 NSMutableString *bodyStr = [NSMutableString string]; 54 [bodyStr appendFormat:@"--%@ ", Boundary]; 55 56 [bodyStr appendFormat:@"Content-Disposition: form-data; name="status" "]; 57 [bodyStr appendFormat:@"%@ ", statusValue]; 58 59 //2.设置accessToken 60 [bodyStr appendFormat:@"--%@ ", Boundary]; 61 62 [bodyStr appendFormat:@"Content-Disposition: form-data; name="access_token" "]; 63 [bodyStr appendFormat:@"%@ ", accessToken]; 64 65 //3.设置Image 66 [bodyStr appendFormat:@"--%@ ", Boundary]; 67 68 [bodyStr appendFormat:@"Content-Disposition: form-data; name="pic"; filename="1.jpg" Content-Type: application/octet-stream "]; 69 70 NSMutableData *totalData = [NSMutableData data]; 71 NSData *textData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding]; 72 73 //拼前面所有字符串 74 [totalData appendData:textData]; 75 76 //拼图片数据 77 [totalData appendData:imgData]; 78 79 NSString *endBoundary = [NSString stringWithFormat:@" --%@-- ", Boundary]; 80 81 [totalData appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]]; 82 83 return totalData; 84 85 86 } 87 88 89 90 - (IBAction)uploadAction:(id)sender { 91 92 NSURL *url = [NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"]; 93 94 //构造request 95 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 96 request.HTTPMethod = @"POST"; 97 request.URL = url; 98 request.timeoutInterval = 60; 99 100 //设置请求头 101 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8;boundary=%@", Boundary]; 102 [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; 103 104 //图片数据 105 NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"]; 106 NSData *imgData = [NSData dataWithContentsOfFile:imgPath]; 107 108 NSData *uploadData = [self buildBodyData:imgData]; 109 110 NSURLSession *session = [NSURLSession sharedSession]; 111 112 NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:uploadData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 113 114 NSLog(@"response:%@", response); 115 116 117 118 }]; 119 120 [uploadTask resume]; 121 122 123 124 } 125 @end