NSURLSession 发送post请求

 1 - (void)test{
 2     //1.构造URL
 3     NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/2/statuses/update.json"];
 4     
 5     //2.构造Request
 6     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 7     
 8     //(1)设置为POST请求
 9     [request setHTTPMethod:@"POST"];
10     
11     //(2)超时
12     //[request setTimeoutInterval:60];
13     
14     //(3)设置请求头
15     //[request setAllHTTPHeaderFields:nil];
16     
17     //(4)设置请求体
18     NSString *bodyStr = @"access_token=xxxxx&status=微博内容";
19     NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
20     
21     //设置请求体
22     [request setHTTPBody:bodyData];
23     
24     //3.构造Session
25     NSURLSession *session = [NSURLSession sharedSession];
26     
27     //4.task
28     NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
29         NSLog(@"response : %@", response);
30     }];
31     
32     //5.
33     [task resume];
34 
35 }

 

posted @ 2016-03-08 10:43  人生路1/5  阅读(220)  评论(0)    收藏  举报