美文网首页
CSRF Failed: CSRF token missing

CSRF Failed: CSRF token missing

作者: 南城同學 | 来源:发表于2020-02-12 22:49 被阅读0次
1. 设置手动管理cookies生命周期 并主动获取;
 manager.requestSerializer = [AFJSONRequestSerializer serializer];
 manager.requestSerializer.HTTPShouldHandleCookies = NO;
 [manager.requestSerializer setValue:[UserManager iz_csrfToken] forHTTPHeaderField:@"Cookie"];
2. 在网络请求成功之后手动保存cookie
    NSURLSessionDataTask *task = [manager POST:self.urlStr
                                    parameters:self.dicParam
                                      progress: nil
                                       success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSHTTPURLResponse* response = (NSHTTPURLResponse* )task.response;
            NSDictionary *allHeaderFieldsDic = response.allHeaderFields;
            NSString *setCookie = allHeaderFieldsDic[@"Set-Cookie"];
            if (setCookie != nil) {
                NSString *cookie = [[setCookie componentsSeparatedByString:@";"] objectAtIndex:0];
                [UserManager iz_saveCsrfTken:cookie];

            }
            [self decodeMessage:responseObject];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    [task resume];
}

相关文章

网友评论

      本文标题:CSRF Failed: CSRF token missing

      本文链接:https://www.haomeiwen.com/subject/frehfhtx.html