Afnetworking版本2内容类型错误



我正在尝试制作一个将与特定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];

,但它给我以下错误与内容类型有关:

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

时,我可以解决此错误

如果使用afnetworking 2.0,则可以使用POST方法,这简化了一点:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:@"https://mycompany.atlassian.net/rest/auth/latest/session/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

这可以创建请求,根据requestSerializer设置设置其Content-Type,并为您编码JSON。afnetworking的优点之一是您可以手动构建和配置NSURLRequest对象的杂草。


顺便说一句,"请求失败:不可接受的内容类型:text/html"错误意味着无论您期望收到什么(例如JSON),您都会收到HTML响应。这很常见:许多服务器错误(例如,服务器通知您该请求已畸形等)生成HTML错误消息。如果您想在failure块中查看HTML,只需记录operation.responseString

事实证明,这是一行:

[request setValue:@"application/json" forHTTPHeaderField:@"Accept" ];

应该是

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type" ];

现在解决了错误。( edit :我应该两个,请参阅Couchdeveloper的评论。)

编辑
Rob的解决方案更好,所以我要使用它。实际上,我尝试了与他所展示的类似解决方案,但是他在哪里有线条,

manager.requestSerializer = [AFJSONRequestSerializer serializer];

我有线:

[manager.requestSerializer
    setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

...它不起作用。值得rob的荣誉使它起作用!

我在afnetworking 2.0中也遇到了相同的问题,解决方案是我必须确保设置afhttprequestserialialializer类型(如果您的请求是JSON),则应该像这样。<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

AFHTTPSessionManager *myHTTPManager = ...
[myManager setRequestSerializer:[AFJSONRequestSerializer serializer]];

您必须同时设置AFHTTPResponseSerializerAFHTTPRequestSerializer

如果您使用的是默认书面类 afappdotnetapiclient ,并且面临此错误。必须添加响应式序列化器。查看您的共享方法应该看起来像 -

+ (instancetype)sharedClient {
static AFAppDotNetAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    _sharedClient = [[AFAppDotNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
    _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    _sharedClient.responseSerializer = [AFHTTPResponseSerializer serializer];
});
return _sharedClient;}

使用以下代码行。

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

相关内容

  • 没有找到相关文章

最新更新