上传数据到微博, 需要在微博开放平台注册一个开发者账号,可以百度,有很多讲解,或者问一下大神。直接看代码,需要的步骤都在其中,注释很详细:
#import "ViewController.h"
//宏定义
#define boundary @"Aab03x"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)uploadTask:(UIButton *)sender {
//01 创建URL
NSURL *url = [NSURL URLWithString:@"https://upload.api.weibo.com/2/statuses/upload.json"];
//02 创建网络请求,这里使用的是NSMutableURLRequest而不是NSURLRequest,是因为父类NSURLRequest的基本设置选项很少
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
//设置请求方式
[request setHTTPMethod:@"POST"];
//设置超时时间
[request setTimeoutInterval:60];
//设置请求头
NSString *content = [NSString stringWithFormat:@"multipart/form-data; charset=utf-8;boundary=%@", boundary];
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置请求体数据
NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"7.jpg" ofType:nil];
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
//调用方法,拿到要上传的网络数据
NSData *data = [self buildData:imgData];
// request.HTTPBody = data;这里不用设置请求体了,因为下面的fromData:data就是请求体!
//03 创建网络会话
NSURLSession *session = [NSURLSession sharedSession];
//04 创建网络会话上传任务
NSURLSessionUploadTask *task = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"发送失败");
}else {
NSLog(@"发送成功");
}
}];
//发起网络请求
[task resume];
}
//上传文件,请求包中数据格式必须如下:
/**
--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--
*/
//需要上传的网络数据(内容)
- (NSData *)buildData:(NSData *)imgData {
//获取令牌
NSString *accessToken = @"2.00RDjyEGC9rz4E4c1d85b38bmm3mFD";
//需要上传的状态值
NSString *statusValues = @"每一个人,都应该被世界倾柔相待。";
//创建可变字符串
NSMutableString *bodyString = [NSMutableString string];
//状态值
[bodyString appendFormat:@"--%@\r\n", boundary];
[bodyString appendFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"];
[bodyString appendFormat:@"%@\r\n", statusValues];
//令牌
[bodyString appendFormat:@"--%@\r\n", boundary];
[bodyString appendFormat:@"Content-disposition: form-data; name=\"access-token\"\r\n\r\n"];
[bodyString appendFormat:@"%@\r\n", accessToken];
//类型name和文件名filename
[bodyString appendFormat:@"--%@\r\n", boundary];
[bodyString appendFormat:@"Content-disposition: form-data; name=\"pic\", fileName=\"file\"\r\n"];
[bodyString appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"];
//图片数据
NSMutableData *allData = [NSMutableData data];
NSData *textData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[allData appendData:textData];
[allData appendData:imgData];
NSString *endString = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
[allData appendData:[endString dataUsingEncoding:NSUTF8StringEncoding]];
//返回所有数据
return allData;
}
@end
此为学习笔记,欢迎提出不足之处。也欢迎和我一样的小白借鉴参考。