NSURLSession(二)POST請求,nsurlsessionpost
//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.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];
話不多說,大家去測試一下吧,通過自己編碼的程式就輕輕鬆鬆的發出微博了。