我正在尝试制作一个与特定JIRA服务器交互的iPhone应用程序.我有以下代码登录:
NSURL *url = [[NSURL alloc] initWithString:@"https://mycompany.atlassian.net/rest/auth/latest/session/"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; NSString *postString = [NSString stringWithFormat:@"{\"username\":\"%@\",\"password\":\"%@\"}", username, password]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept" ]; operation.responseSerializer = [AFJSONResponseSerializer serializer]; [operation setCompletionBlockWithSuccess: ^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"ERROR: %@", error); } ]; [operation start];
但它给了我以下与Content-Type有关的错误:
ERROR: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unsupported media type (415)" UserInfo=0x8cd6540 { NSErrorFailingURLKey=https://mycompany.atlassian.net/rest/auth/latest/session/, NSLocalizedDescription=Request failed: unsupported media type (415), NSUnderlyingError=0x8c72e70 "Request failed: unacceptable content-type: text/html",
我不确定问题是什么.我发现这个问题,我认为可能是一个类似的问题,但答案是要么使用AFJSONRequestOperation
该类(我不能,因为我使用AFNetworking版本2,没有该类),或修复它在服务器端(我也不能出于显而易见的原因).
当我无法修复服务器端而无法使用时,我可以修复此错误AFJSONRequestOperation
?