我正在iOS上使用AFNetworking并试图访问给我一个302状态代码的网页,重定向我,然后在重定向页面加载时重定向200.
但是,我无法在我的iOS应用中捕捉到这一点。我尝试添加authenticationChallenge块但不会被调用。 completionBlock正在获取重定向视图,但状态代码仅为200。我在这里错过了什么?当我得到302代码时,我需要发送登录详细信息。我的代码粘贴在下面:
AFHTTPRequestOperation *uploadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
[uploadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Request: %@", [operation.request description]);
NSLog(@"CODE: %i",operation.response.statusCode);
NSLog(@"Response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
self.statusLabel.text = @"upload failed";
}];
[uploadOperation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
NSLog(@"pls authenticate");
}];
答案 0 :(得分:3)
知道了。我正在设置错误的块。验证挑战不是Django应用程序在@login_required装饰器的情况下发送的。这是需要设置的redirectResponse!
[uploadOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"pls authenticate");
return request;
}];