AFNetworking 发送 header 请求

AFNetworking send header with request

我搜索了几个小时,找到了像 this 这样的答案: 但是当我像这样发送授权时:

    AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        //header = [header stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

        //[manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Accept"];
        //[manager.requestSerializer setValue:@"text/json" forHTTPHeaderField:@"Content-Type"];
        [manager.requestSerializer setValue:header forHTTPHeaderField:@"Authorization"];
        /*
        NSLog(@"headers: %@",manager.requestSerializer.HTTPRequestHeaders);
        this printed : {
            "Accept-Language" = "en;q=1";
            Authorization =  "Bearer eyJ0eXAiO";
            "User-Agent" = "Virtuocity/1.0 (iPhone; iOS 10.2; Scale/2.00)";
        }
        */
        [manager GET:url parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            block(responseObject,nil);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            block(nil,error);
        }];

但是服务器给我以下错误:

serialization.response Code=-1011 "Request failed: unauthorized (401)  headers {
    "Access-Control-Allow-Headers" = "accept, accept-language, content-type, accept, authorization, moduleid, tabid, x-dnn-moniker";
    "Access-Control-Allow-Methods" = "GET, POST, PUT, HEAD, OPTIONS";
    "Access-Control-Allow-Origin" = "*";
    "Cache-Control" = "no-cache";
    "Content-Length" = 61;
    "Content-Type" = "text/json; charset=utf-8";
    Date = "Sun, 29 Jan 2017 10:18:36 GMT";
    Expires = "-1";
    Pragma = "no-cache";
    "Set-Cookie" = "dnn_IsMobile=False; path=/; HttpOnly, dnn_IsMobile=False; path=/; HttpOnly, language=en-US; path=/; HttpOnly";
}

我观察到错误返回的 headers 不包括我发送给他们的 'Authorization' header,我怀疑这是因为白色 space 在 header 中发送,所以我不知道出了什么问题,我在 POST MAN 上尝试了相同的请求并且工作完美所以现在该怎么做?

我发现 AFNetworking 默认发送某种 cookie,而我正在处理的 api 如果有 cookie 需要检查,则不会检查授权,所以我禁用了使用此行发送 cookie 和它有效 ^__^

[manager.requestSerializer setHTTPShouldHandleCookies:NO];