发布请求AFNetworking上的Http Header身份验证

时间:2013-07-10 16:59:34

标签: objective-c post file-upload afnetworking basic-authentication

我正在尝试使用AFNetworking的AFHttpClient创建multipartFormRequestWithMethod。它必须使用Http Authorization Header通过ASP.NET REST服务进行身份验证。因此,我使用setAuthorizationHeaderWithUsername:password:将用户名和密码传递给Http授权标头。在请求的正文中,我传递了一个大文件,几个MB。如果Header身份验证失败,我希望请求在完成文件发送之前获取阻止并进入失败状态。在认证失败的情况下应该有一些阻止数据库发送的东西,但我无法弄清楚是什么。在当前情况下,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];

0 个答案:

没有答案