主要讲解AFHTTPRequestSerializer提供的三种创建NSMutableURLRequest的方式的第三种,前两种已经在之前介绍过,此方法专门是针对Amazon S3产生的问题解决
/**
创建一个NSMutableURLRequest并删除request的HTTPBodyStream,在请求结束的回调中把数据异步的方式写到制定的文件中
Creates an `NSMutableURLRequest` by removing the `HTTPBodyStream` from a request, and asynchronously writing its contents into the specified file, invoking the completion handler when finished.
@param request The multipart form request. The `HTTPBodyStream` property of `request` must not be `nil`. //request对象不能为nil
@param fileURL The file URL to write multipart form contents to.
@param handler A handler block to execute.
在与Amazon S3服务交互时会出现当请求体内容为streaming时会导致请求头无法发送Content-Length字段,解决办法是使用下面的方法创建request请求取代通过上面你的方式创建的请求或者其他的带有httpbodystream的方式的request,创建一个原始请求对象的副本,并把HTTPBodyStream设置为nil。
@discussion There is a bug in `NSURLSessionTask` that causes requests to not send a `Content-Length` header when streaming contents from an HTTP body, which is notably problematic when interacting with the Amazon S3 webservice. As a workaround, this method takes a request constructed with `multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:error:`, or any other request with an `HTTPBodyStream`, writes the contents to the specified file and returns a copy of the original request with the `HTTPBodyStream` property set to `nil`. From here, the file can either be passed to `AFURLSessionManager -uploadTaskWithRequest:fromFile:progress:completionHandler:`, or have its contents read into an `NSData` that's assigned to the `HTTPBody` property of the request.
@see https://github.com/AFNetworking/AFNetworking/issues/1398
*/
- (NSMutableURLRequest *)requestWithMultipartFormRequest:(NSURLRequest *)request
writingStreamContentsToFile:(NSURL *)fileURL
completionHandler:(nullable void (^)(NSError * _Nullable error))handler{
NSParameterAssert(request.HTTPBodyStream);
NSParameterAssert([fileURL isFileURL]);
//获取原始请求对象的HTTPBodyStream;
NSInputStream *inputStream = request.HTTPBodyStream;
//根据请求url创建NSOutputStream
NSOutputStream *outputStream = [[NSOutputStream alloc] initWithURL:fileURL append:NO];
__block NSError *error = nil;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//指配输入输出流的runloopmode
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
//配置好输入输出流之后打开输入输出流,开始读写数据
[inputStream open];
[outputStream open];
//读写数据逻辑
while ([inputStream hasBytesAvailable] && [outputStream hasSpaceAvailable]) {
uint8_t buffer[1024];
//向内存中写入数据
NSInteger bytesRead = [inputStream read:buffer maxLength:1024];
if (inputStream.streamError || bytesRead < 0) {
error = inputStream.streamError;
break;
}
//从内存中读数据到远程url
NSInteger bytesWritten = [outputStream write:buffer maxLength:(NSUInteger)bytesRead];
if (outputStream.streamError || bytesWritten < 0) {
error = outputStream.streamError;
break;
}
if (bytesRead == 0 && bytesWritten == 0) {
break;
}
}
//读写结束之后关闭流
[outputStream close];
[inputStream close];
//完成之后回调成功
if (handler) {
dispatch_async(dispatch_get_main_queue(), ^{
handler(error);
});
}
});
//创建request的副本
NSMutableURLRequest *mutableRequest = [request mutableCopy];
mutableRequest.HTTPBodyStream = nil;
return mutableRequest;
}