AFNetworking Library - 如何在上传图像时传递基本身份验证?

时间:2014-04-24 13:35:30

标签: ios afnetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"..." password:@"..."];

NSDictionary *parameters = @{@"key": @"value"};

NSData *dataForFile = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"a" ofType:@"jpg"]];

[manager POST:@"https:myURL.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //[formData appendPartWithFileURL:filePath name:@"image" error:nil];

    [formData appendPartWithFileData:dataForFile name:@"a.jpg" fileName:@"a.jpg" mimeType:@"image/jpg"];
    [formData appendPartWithFileData:dataForFile name:@"a1.jpg" fileName:@"a1.jpg" mimeType:@"image/jpg"];
    [formData appendPartWithFileData:dataForFile name:@"a2.jpg" fileName:@"a2.jpg" mimeType:@"image/jpg"];
    [formData appendPartWithFileData:dataForFile name:@"a3.jpg" fileName:@"a3.jpg" mimeType:@"image/jpg"];
    [formData appendPartWithFileData:dataForFile name:@"a4.jpg" fileName:@"a4.jpg" mimeType:@"image/jpg"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

即使在设置了requestSerializer身份验证参数之后,也无法传递它,因为无法上传图像。

有人试图将图像上传到经过身份验证的服务器。

0 个答案:

没有答案