我正在尝试从我们的S3存储中下载PDF文件。这些文件是公共的,可以通过浏览器访问。
使用AFNetworking时,我收到403。代码如下所示
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:self.pdfUrl]];
void (^success)(AFHTTPRequestOperation*, id) = ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"WE HAS SUCCESS :D");
};
void (^failure)(AFHTTPRequestOperation*, id) = ^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"failure :((. OPERATION:\n\n%@ \n\nResponseObject:\n\n%@",operation,responseObject);
};
NSURLRequest *request = [client requestWithMethod:@"GET" path:@"" parameters:nil];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:[TTConstants temporaryFilePath] append:NO];
[client enqueueHTTPRequestOperation:operation];
从而产生以下产出:
failure :((. OPERATION:
<AFHTTPRequestOperation: 0x95a2f40, state: isFinished, cancelled: NO request:
<NSMutableURLRequest https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/>,
response: <NSHTTPURLResponse: 0xa692480>>
ResponseObject:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 403"
UserInfo=0xa6bda40 {AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/>,
NSErrorFailingURLKey=https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c/pdfnumberone.pdf/,
NSLocalizedDescription=Expected status code in (200-299), got 403,
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xa692480>}
建议会很棒!
1条答案
按热度按时间wwodge7n1#
不要使用完整的PDF URL作为
AFHTTPClient
的baseURL
,因为这会导致最终的URL在末尾有额外的/
。相反,将baseURL
设置为https://s3.amazonaws.com/com.turtletail.java-bucket/dbda2ccb-91db-4672-9ad4-288dd313ef8c
,然后在请求中将路径设置为pdfnumberone.pdf
。