1

我正在尝试multipartFormRequestWithMethod使用 AFNetworking 的 AFHttpClient 创建一个。它必须使用 Http Authorization Header 通过 ASP.NET REST 服务进行身份验证。因此,我使用setAuthorizationHeaderWithUsername:password:. 在请求的正文中,我传递了一个大文件,几 MB。如果标头身份验证失败,我希望请求在完成文件发送之前获得阻塞并进入失败状态。如果身份验证失败,应该有一些东西可以阻止数据库发送,但我不知道是什么。在当前情况下,AFHttpRequestOperation 开始发送文件并仅在文件发送结束时通知错误。这是代码:

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://v-moxdevelop/MOX.UploadDBService/UploadDB/"]];

NSString *psw = @"psw";
NSString *userName = @"username";

[httpClient setAuthorizationHeaderWithUsername:selectedLoginItem.user password:@"psw"];

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path: [NSString stringWithFormat:@"Upload/%@/%@/%@", UDID, [selectedLoginItem.firm stringByReplacingOccurrencesOfString:@"." withString:@""], [selectedLoginItem.user stringByReplacingOccurrencesOfString:@"." withString:@""]]  parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:fileData name:zipFilePath fileName:[zipFilePath lastPathComponent] mimeType:@"application/zip"];
}];

[request setTimeoutInterval:INT32_MAX];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];



operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];


__weak typeof(self) weakSelf = self;
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
}];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *locOperation, id responseObject) {
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {

 }];



[httpClient enqueueHTTPRequestOperation:operation];
4

0 回答 0