ios - 在 AFNetwoking 3.0 中上传 Zip 文件

标签 ios objective-c iphone afnetworking-3

我正在使用 AFNetworking 3.0 在服务器上上传 Zip 文件,但它从服务器端给出了以下错误。

代码

AFHTTPSessionManager  *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www,baseurl.com”]];


    [manager.requestSerializer setValue:@“headertoeknvalue” forHTTPHeaderField:@"Authorization"];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];


    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

    [ manager POST:@"sync.php" parameters:dictPost constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        NSData *data = [[NSFileManager defaultManager] contentsAtPath:[APP_DELEGATE.strDocumentDirectoryPath stringByAppendingString:@"/Database.zip"]];

 [formData appendPartWithFileData:data
                                    name:@"sync_file"
                                fileName:@"Database.zip" mimeType:@"application/json"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"Response: %@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"error: %@", error.description);

    }];

错误

[Error] POST  (401) [7.6721 s]: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x79735300> { URL:  } { status code: 401, headers {
    "Cache-Control" = "no-cache";
    Connection = "Keep-Alive";
    "Content-Type" = "application/json";
    Date = "Mon, 05 Sep 2016 06:32:54 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/1.0.1e-fips mod_bwlimited/1.4";
    "Transfer-Encoding" = Identity;
    "X-Powered-By" = "PHP/5.5.38";
} }, NSErrorFailingURLKey=, NSLocalizedDescription=Request failed: unauthorized (401), com.alamofire.serialization.response.error.data=<7b227374 61747573 223a6661 6c73652c 22737461 74757343 6f646522 3a343031 2c226d65 73736167 65223a22 546f6b65 6e206162 73656e74 227d>}

最佳答案

401 错误 Code=-1011 "Request failed: unauthorized (401)" 检查您的 token 是否有效

你的问题是在我们序列化请求之前交换你传递的授权行,所以你需要交换如下行

AFHTTPSessionManager  *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www,baseurl.com”]];

manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager.requestSerializer setValue:@“headertoeknvalue” forHTTPHeaderField:@"Authorization"];

关于ios - 在 AFNetwoking 3.0 中上传 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325058/

相关文章:

iphone - iOS 中无休止的重复滚动/动画背景?

ios - SKShapeNode 位置和 touch.location 的区别

ios - EKCalendarEKCalendar CGcolor未在iPhone日历中设置颜色

iphone - 继续添加到 NSMutableArray

php - 显示本地存储的图像以响应 JSON 表填充结果

ios - Xcode - 未找到从 Main.storyboard 引用的名为 "MyStoryboard"的 Storyboard

ios - 通过多个按钮播放多个音频文件

ios - 正确地将 info.plist 条目转换为 NS* 类型

ios - NSURLSession 中的委托(delegate)和完成处理程序

iphone - 如何在我的MKMapView中添加街景功能?