简短的背景故事,我们以前的开发人员使用ASIHTTPRequest发出POST请求并从我们的Web服务中检索数据.由于未知原因,我们的应用程序部分停止工作.似乎有足够的时间来进行未来的证明并与AFNetworking一起使用.REST webservice在CakePHP框架上运行.
简而言之,我没有使用AFNetworking接收请求响应字符串.
我知道web服务有效,因为我能够成功发布数据并使用curl接收正确的响应:curl -d"data [Model] [field0] = field0value&data [Model] [field1] = field1value" https://example.com /api/class/function.plist
根据之前开发人员的说明,我提出了以下建议.
#import "AFHTTPRequestOperation.h"
…
- (IBAction)loginButtonPressed {
NSURL *url = [NSURL URLWithString:@"https://example.com/api/class/function.plist"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[usernameTextField text] forHTTPHeaderField:@"data[User][email]"];
[request setValue:[passwordTextField text] forHTTPHeaderField:@"data[User][password]"];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
NSLog(@"response string: %@ ", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
[operation start]; …Run Code Online (Sandbox Code Playgroud)