6

Ho bisogno di caricare file video dalla mia app su un server. Ho provato a farlo tramite [AFHTTPRequestOperationManager post:parameters:success:failure] ma purtroppo ho continuato a ricevere i timeout delle richieste. Ora sto provando qualcosa di simile a Creazione di un'attività di caricamento da the AF Docs.AF Attività di upload in rete con credenziali (autenticazione di base HTTP)

ho letto on SO e la AF Docs su setSessionDidReceiveAuthenticationChallengeBlock: e cercato di attuare l'intero caricati malarky come segue:

__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]; 

L'oggetto myself.credentials è stata impostata in precedenza per avere il nome utente e la password corretti. Ogni volta che questa richiesta viene attivata, ottengo 401 unauthorised come risposta. Ho provato a inserire NSLog(@"CHALLENGE") all'interno del blocco sfida sopra, ma non sembra mai essere chiamato, quindi AFNetworking non mi sta dando un modo per fornire le credenziali. So che funziona perfettamente sul lato server perché l'ho testato con Postman.

Come posso ottenere AFNetworking per consentirmi di fornire credenziali per l'autenticazione di base HTTP con questa attività di caricamento?

risposta

0

io non sono sicuro di AFNetworking di setSessionDidReceiveAuthenticationChallengeBlock, ma finché si dispone di un NSMutableURLRequest si può impostare l'intestazione Autorizzazione HTTP direttamente sull'oggetto richiesta:

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

Tenete a mente che il valore deve essere la rappresentazione base64 della stringa username:password.

In caso contrario, per richieste GET, POST, ecc. Su un gestore sessioni, è possibile impostare le credenziali sul serializzatore della richiesta utilizzato dal gestore sessioni. Vedere:

[AFHTTPRequestSerializer setAuthorizationHeaderFieldWithUsername:password:] [AFHTTPRequestSerializer clearAuthorizationHeader]

Problemi correlati