Reputation: 24857
I have an iOS application using AFNetworking for the network calls. Everything works great except when I am trying to capture a 401 status code. It always takes about 60 seconds for the call to return with a response. Is there any setting or something that would cause this? Every successful call returns instantly. I have also tried running this code through curl and the 401 returns instantly. Here is the code I am using:
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
__weak AFHTTPRequestOperation *_operation = operation;
[operation setCompletionBlock:^{
//It takes 60 seconds to get to this line if a 401 is encountered
DDLogInfo(@"Response body: %@", [_operation responseString]);
if(_operation.response.statusCode == 401) {
DDLogInfo(@"Expired Auth Token");
}
NSError *error = [_operation error];
NSData *responseData = [_operation responseData];
if(responseData == nil && error == nil)
error = [NSError errorWithDomain:isDevMode ? DEV_SERVICE_BASE_URL : SERVICE_BASE_URL code:-1 userInfo:nil];
callback(error, responseData);
}];
[operation start];
Upvotes: 2
Views: 3052
Reputation: 3154
When the request fails with the code 401, the failure block of the operation is called, which you haven't set. Use the following method of AFHTTPRequestOperation for your operation and the failure block will be called instantaneously.
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
Sorry, didn't notice this thread was 6 months old! Anyhow, for those who are still facing the problem, the answer remains.
Upvotes: 0
Reputation: 27597
You need to initialize your NSURLRequest with a timeout value that fits your needs.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:inURL
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:10.0];
See the NSMutableURLRequest documentation.
Then, initialize AFNetworking's AFHTTPRequestOperation
with that initialized NSMutableURLRequest
request.
Upvotes: 2