我需要從我的應用程序上傳視頻文件到服務器。我試圖通過[AFHTTPRequestOperationManager post:parameters:success:failure]
這樣做,但不幸的是不斷收到請求超時。我現在正在嘗試類似於從the AF Docs創建上傳任務。AFNetworking上傳任務與憑據(HTTP基本認證)
我讀on SO和AF Docs約setSessionDidReceiveAuthenticationChallengeBlock:
,並試圖實現整個上傳說大話如下:
__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進行了測試。
我如何獲得AFNetworking讓我提供憑藉此HTTP上傳任務的HTTP基本身份驗證憑據?