AFNetworking 2.0上传图片时的错误信息如下:
Error Domain=NSURLErrorDomain Code=-1021 "request body stream exhausted" UserInfo={NSUnderlyingError=0x1522c29d0 {Error Domain=kCFErrorDomainCFNetwork Code=-1021 "request body stream exhausted" UserInfo={NSErrorFailingURLStringKey=http://******/resource_upload, NSLocalizedDescription=request body stream exhausted, NSErrorFailingURLKey=http://******/resource_upload}}, NSErrorFailingURLStringKey=http://******/resource_upload, NSErrorFailingURLKey=http://******/resource_upload, NSLocalizedDescription=request body stream exhausted}
解决方案:
直接添加一个AFURLConnectionOperation的category类,实现如下代码即OK。
#import "AFURLConnectionOperation+AuthenticationChallengeUploadFix.h"
@implementation AFURLConnectionOperation (AuthenticationChallengeUploadFix)
- (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request {
//苹果默认网络请求缓冲区复用,当缓冲区溢出后需要重新开辟缓冲区。否则溢出的数据无法上传服务器。
if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
return [request.HTTPBodyStream copy];
}
return nil;
}
@end