我在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");
}];发布于 2013-04-06 03:21:20
明白了。我找错地方了。身份验证质询不是Django应用程序在@login_required装饰器中发送的。需要设置的是redirectResponse!
[uploadOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"pls authenticate");
return request;
}];https://stackoverflow.com/questions/15840519
复制相似问题