afnetworking 3.0 How to POST with headers and HTTP Body
来源:互联网 发布:软横跨计算软件 编辑:程序博客网 时间:2024/06/08 14:20
转自:http://www.jianshu.com/p/6a61812aedd2
一般请求的话是这样
AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil]; [session POST:@"" parameters:@"" progress:nil success:nil failure:nil];
但是今天遇到个不管怎么样parameters都传不到服务器那边,而后台那边给我的解释是传输用[request setHTTPBody:data];
而 afn 2.0 是这样的
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];[request setHTTPMethod:@"POST"];[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];NSError *error;NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error];NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];// And finally, add it to HTTP body and job done.[request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];manager.requestSerializer.timeoutInterval=[[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue];manager.requestSerializer = [AFJSONRequestSerializer serializer];AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request progress:nil success:^(NSURLSessionTask *task, id responseObject) { NSLog(@"Reply JSON: %@", responseObject); }} failure:^(NSURLSessionTask *operation, NSError *error) { NSLog(@"Error: %@, %@, %@, %@, %@", error, operation.responseObject, operation.responseData, operation.responseString, operation.request);}];[operation start];
但是3.0弃用了AFHTTPRequestOperationManager,那么怎么才能用呢?
1.用系统NSURLConnection 请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"url"]]; [request setHTTPBody:data]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; if (theConnection) { NSLog(@"connected"); receivedData=[[NSMutableData alloc]init]; } else { NSLog(@"not connected"); }-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [receivedData setLength:0];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [receivedData appendData:data]; NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"response: %@",responseString);}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"Succeeded! Received %lu data",(unsigned long)[receivedData length]); NSString* responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding]; NSLog(@"response: %@",responseString); NSError *myError = nil; NSDictionary *res = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableLeaves error:&myError]; NSLog(@"%@",res);}
2.用不习惯系统没关系,其实afn3.0 也有提供api给我们调用,但是这个是我一直没接触过的AFURLSessionManager。
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:@"url" parameters:nil error:nil]; req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue]; [req setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [req setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [req setHTTPBody:da]; [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { if (!error) { NSLog(@"Reply JSON: %@", responseObject); } else { NSLog(@"Error: %@, %@, %@", error, response, responseObject); } }] resume];
阅读全文
0 0
- afnetworking 3.0 How to POST with headers and HTTP Body
- AFNetWorking post body内容
- How to use GET and POST methods in HTTP from a MIDlet
- How to read email headers
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- How to configure XDB for using ftp and http protocols with ASM [ID 357714.1]
- How to disable 'withcredentials' in HTTP header with node.js and Request package?
- How to use http cookies with Qt
- How to use http cookies with Qt
- C# to POST HTTP with XML
- Force Download with HTTP Headers
- How to post JSON to PHP with curl
- FMC http post 添加 body
- Quick reference to HTTP headers
- Quick reference to HTTP headers
- How to post SO's or PO's with X++
- how to post URL with Image on facebook by ShareKit
- How to POST a django form with AJAX & jQuery
- 适配器模式
- 六、swift3.0类的使用
- IP广播
- WAMPServer集成环境下载跟安装
- HDOJ 2031 进制转换
- afnetworking 3.0 How to POST with headers and HTTP Body
- EffectiveC++学习笔记-条款9
- codeforces712
- 你所不知道的AWS 云服务清单(71种)
- 【Unity光照系统】之实时和烘焙光照
- Codeforces Round #419 (Div. 2)Karen and Coffee
- 非凸问题与凸问题求解
- 为什么使用 Linux 和开源软件?
- 【Machine Learning】使用随机森林进行特征选择