这里需要帮助,我正在尝试将文件上传到安全 API,我能够使用 Advanced REST Client chrome 扩展成功上传,但是当我尝试将其转换为 afnetworking 发布请求时,它总是返回请求超时错误,我已经这样做了 3 天,但我总是收到请求超时错误,尝试延长超时间隔但无济于事。
我的代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[[CookieHandler sharedManager] cookieEaterUsingCookiesInURL:OC_BASE_URL];
NSString *csrftoken = [[CookieHandler sharedManager] getCSRFToken];
[manager.requestSerializer setValue:csrftoken forHTTPHeaderField:COOKIES_CSRFTOKEN_KEY];
[manager.requestSerializer setTimeoutInterval:120];
AFHTTPRequestOperation *operation = [manager POST:[self composeFullUrlWithRestApi:uploadUrl] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:fileData name:@"fileUpload" fileName:documentName mimeType:mimeType];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
NSLog(@"Wrote %ld/%ld", (long)totalBytesWritten, (long)totalBytesExpectedToWrite);
}];
打印operation
变量产量:
<AFHTTPRequestOperation: 0x15731040, state: isExecuting, cancelled: NO request: <NSMutableURLRequest: 0x15523180> { URL: https://preprod.myUrl.com/uploadApi, headers: {
"Accept-Language" = "en;q=1";
CSRFToken = "cfbaf91f-6f2b-4ed1-a094-06c8eec6f757";
"Content-Length" = 32943;
"Content-Type" = "multipart/form-data; boundary=Boundary+B7082B5EB6A9063A";
"User-Agent" = "ObjectiveConnect/5 (iPhone; iOS 8.0.2; Scale/2.00)";
} }, response: (null)>
打印错误产生:
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
UserInfo=0x1561fec0 {NSErrorFailingURLStringKey=https://preprod.myUrl.com/uploadApi,
NSErrorFailingURLKey=https://preprod.myUrl.com/uploadApi,
NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1567c400 "The request timed out."}