6

내 앱에서 서버로 비디오 파일을 업로드해야합니다. 나는 [AFHTTPRequestOperationManager post:parameters:success:failure]을 통해 그렇게 시도했지만 불행히도 요청 시간 초과가 계속 발생했습니다. 지금은 업로드 작업 만들기와 비슷한 것을 시도하고 있습니다.the AF Docs입니다. AFNetworking 자격 증명을 사용하여 업로드 작업 (HTTP Basic Auth)

나는 약 setSessionDidReceiveAuthenticationChallengeBlock: on SOAF Docs을 읽고 다음과 같이 전체 업로드 malarky을 구현하기 위해 노력 :

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

myself.credentials 객체가 올바른 사용자 이름과 암호를 가지고 이전에 설정되었습니다. 이 요청이 발생할 때마다 401 unauthorised을 응답으로받습니다. 위의 챌린지 블록에 NSLog(@"CHALLENGE")을 넣으려고 시도했지만 결코 호출되지 않는 것 같습니다. 그래서 AFNetworking은 나에게 자격 증명을 제공하는 방법을 제공하지 않습니다. 저는 이것이 Postman과 테스트를했기 때문에 이것이 서버 측에서 완벽하게 작동한다는 것을 압니다.

이 업로드 작업으로 HTTP 기본 인증에 대한 자격 증명을 제공하도록 AFNetworking을 어떻게받을 수 있습니까?

답변

0

나는 AFNetworking의 setSessionDidReceiveAuthenticationChallengeBlock에 대해 잘 모르겠지만, 한 당신은 NSMutableURLRequest을 가지고 당신은 Request 오브젝트에 대해 직접 인증 HTTP 헤더를 설정할 수 있습니다 :

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

이 값이해야한다는 것을 명심하십시오 문자열은 username:password의 base64 표현입니다.

그렇지 않으면 세션 관리자에서 GET, POST 등의 요청에 대해 세션 관리자가 사용하는 요청 직렬기에 자격 증명을 설정할 수 있습니다. 참조 :

[AFHTTPRequestSerializer setAuthorizationHeaderFieldWithUsername:password:] [AFHTTPRequestSerializer clearAuthorizationHeader]