您的位置:首页 > 其它

NSURLSession(二)POST请求

2016-01-26 09:48 218 查看
//1.构造URL

NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"];

//2.构造Request

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//(1)设置为POST请求

[request setHTTPMethod:@"POST"];

//(2)超时

[request setTimeoutInterval:60];

//(3)设置请求头

//[request setAllHTTPHeaderFields:nil];

//(4)设置请求体

//发新浪微博

//请求体里需要包含至少两个参数

//指定用户的令牌 微博正文

//access_token status

//这里的 access_token 大家可以用自己的微博来测试 access_token->是通过自己的微博账号密码生成的 具体流程可以参照 http://www.cnblogs.com/ok-lanyan/archive/2012/07/15/2592070. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html

NSString *bodyStr = @"access_token=xxxxx&status=微博内容";

NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

//设置请求体

[request setHTTPBody:bodyData];

//3.构造Session

NSURLSession *session = [NSURLSession sharedSession];

//4.task

NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSLog(@"response : %@", response);

}];

//5.

[task resume];

话不多说,大家去测试一下吧,通过自己编码的程序就轻轻松松的发出微博了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: