6

Muszę przesłać pliki wideo z mojej aplikacji na serwer. Próbowałem to zrobić przez [AFHTTPRequestOperationManager post:parameters:success:failure], ale niestety ciągle uzyskiwałem limity czasu żądania. Próbuję teraz czegoś podobnego do Tworzenie zadania przesyłania z the AF Docs.AFNetworking Prześlij zadanie z poświadczeniami (HTTP Basic Auth)

czytam on SO i AF Docs o setSessionDidReceiveAuthenticationChallengeBlock: i starał się realizować cały wysyłania malarky następująco:

__block ApiManager *myself = self; 

// Construct the URL 
NSString *strUrl = [NSString stringWithFormat:@"%@/%@", defaultUrl, [self getPathForEndpoint:endpoint]]; 
NSURL *URL = [NSURL URLWithString:strUrl]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 
[request setHTTPMethod:@"POST"]; 

// Build a session manager 
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

// Set authentication handler 
[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *credential) { 
    *credential = myself.credentials; 
    return NSURLSessionAuthChallengeUseCredential; 
}]; 


// Create the upload task 
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
    if (error) { 
     [myself endpoint:endpoint returnedFailure:error]; 
    } else { 
     [myself endpoint:endpoint returnedSuccess:responseObject]; 
    } 
}]; 

// and run with it 
[uploadTask resume]; 

Przedmiotem myself.credentials został ustawiony wcześniej mieć poprawny login i hasło. Za każdym razem, gdy żądanie to zostanie wywołane, otrzymuję odpowiedź 401 unauthorised. Próbowałem umieścić NSLog(@"CHALLENGE") w bloku wyzwań powyżej, ale nigdy nie wydaje się, aby uzyskać wywołanie, więc AFNetworking nie daje mi sposobu na dostarczenie referencji. Wiem, że działa to doskonale po stronie serwera, ponieważ testowałem go z Postmanem.

W jaki sposób mogę uzyskać AFNetworking, aby umożliwić mi dostarczanie poświadczeń dla HTTP Basic Auth z tym zadaniem przesyłania?

Odpowiedz

0

nie jestem pewien o AFNetworking na setSessionDidReceiveAuthenticationChallengeBlock, ale tak długo, jak masz NSMutableURLRequest można ustawić nagłówek autoryzacji HTTP bezpośrednio na obiekcie żądania:

[request setValue:base64AuthorizationString forHTTPHeaderField:@"Authorization"];

pamiętać, że wartość musi być reprezentacją base64 ciągu username:password.

W pozostałych przypadkach, w przypadku żądań GET, POST itp. W menedżerze sesji, można ustawić poświadczenia w serializatorze żądań używanym przez menedżera sesji. Zobacz:

[AFHTTPRequestSerializer setAuthorizationHeaderFieldWithUsername:password:] [AFHTTPRequestSerializer clearAuthorizationHeader]

Powiązane problemy