如何读取 HEAD 请求的 http 状态码
How to read http status code for HEAD request
我正在使用 AFNetworking 4.3.1 和 Http 方法 GET 和 HEAD 请求 API。它可以 return 不同的 http 状态代码(100、200、402、..)
我的 get 函数请求工作正常。
-(void) getApiV2:(NSString *)url
parameters:(NSDictionary *)param
success:(successBlock)mSuccessBlock
failure:(failureBlock)mfailureBlock{
NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
if(TOKEN !=nil){
[httpHeader setObject:TOKEN forKey:@"Token"];
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
GET:url
parameters:param
headers:httpHeader
progress:nil
success: ^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
int statusCode = [[responseObject objectForKey:@"code"] intValue];
NSArray *data = [responseObject objectForKey:@"data"];
BLOCK_SAFE_RUN(mSuccessBlock,data,statusCode);
}
failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
BLOCK_SAFE_RUN(mfailureBlock,error);
}
];
};
但是我的 head 函数抛出错误。
-(void) head:(NSString *)url success:(successBlock)mSuccessBlock failure:(failureBlock)mfailureBlock{
NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
if(TOKEN !=nil){
[httpHeader setObject:TOKEN forKey:@"Token"];
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
HEAD:url
parameters:nil
headers:httpHeader
success: ^(id _Nullable responseObject){
int statusCode = [[responseObject objectForKey:@"code"] intValue];
BLOCK_SAFE_RUN(mSuccessBlock,responseObject,statusCode);
}
failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
BLOCK_SAFE_RUN(mfailureBlock,error);
}
];
};
在运行的时候,我在int statusCode = [[responseObject objectForKey:@"code"] intValue];
行出现了这个错误
[__NSCFLocalDataTask objectForKey:]: unrecognized selector sent to instance 0x110f88860
服务器正确处理请求并完成工作。
我尝试其他语法:
int statusCode = (int) [responseObject statusCode];
'NSInvalidArgumentException', reason: '-[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x145731af0'
来自
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)responseObject;
int statusCode = (int) httpResponse.statusCode;
[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x129665ed0
使用 AFNetworking 获取 HEAD 请求的 http 状态代码的好方法是什么?
根据@Larme 的建议并由 source code of AFNetworking、HEAD:parameters:headers:success:failure:
return a NSURLSessionDataTask
确认
所以我需要检索 NSHTTPURLResponse
才能获得 statusCode
。
NSURLSessionTask *task = (NSURLSessionTask *)responseObject;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)[task response];
int statusCode = (int) httpResponse.statusCode;
NSLog(@"Status code: %d", statusCode);
我正在使用 AFNetworking 4.3.1 和 Http 方法 GET 和 HEAD 请求 API。它可以 return 不同的 http 状态代码(100、200、402、..)
我的 get 函数请求工作正常。
-(void) getApiV2:(NSString *)url
parameters:(NSDictionary *)param
success:(successBlock)mSuccessBlock
failure:(failureBlock)mfailureBlock{
NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
if(TOKEN !=nil){
[httpHeader setObject:TOKEN forKey:@"Token"];
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
GET:url
parameters:param
headers:httpHeader
progress:nil
success: ^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject){
int statusCode = [[responseObject objectForKey:@"code"] intValue];
NSArray *data = [responseObject objectForKey:@"data"];
BLOCK_SAFE_RUN(mSuccessBlock,data,statusCode);
}
failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
BLOCK_SAFE_RUN(mfailureBlock,error);
}
];
};
但是我的 head 函数抛出错误。
-(void) head:(NSString *)url success:(successBlock)mSuccessBlock failure:(failureBlock)mfailureBlock{
NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
if(TOKEN !=nil){
[httpHeader setObject:TOKEN forKey:@"Token"];
}
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager
HEAD:url
parameters:nil
headers:httpHeader
success: ^(id _Nullable responseObject){
int statusCode = [[responseObject objectForKey:@"code"] intValue];
BLOCK_SAFE_RUN(mSuccessBlock,responseObject,statusCode);
}
failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
BLOCK_SAFE_RUN(mfailureBlock,error);
}
];
};
在运行的时候,我在int statusCode = [[responseObject objectForKey:@"code"] intValue];
[__NSCFLocalDataTask objectForKey:]: unrecognized selector sent to instance 0x110f88860
服务器正确处理请求并完成工作。
我尝试其他语法:
int statusCode = (int) [responseObject statusCode];
'NSInvalidArgumentException', reason: '-[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x145731af0'
来自
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)responseObject;
int statusCode = (int) httpResponse.statusCode;
[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x129665ed0
使用 AFNetworking 获取 HEAD 请求的 http 状态代码的好方法是什么?
根据@Larme 的建议并由 source code of AFNetworking、HEAD:parameters:headers:success:failure:
return a NSURLSessionDataTask
所以我需要检索 NSHTTPURLResponse
才能获得 statusCode
。
NSURLSessionTask *task = (NSURLSessionTask *)responseObject;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)[task response];
int statusCode = (int) httpResponse.statusCode;
NSLog(@"Status code: %d", statusCode);