I have been looking for a good answer for this damn problem for over 10 hours and finally figured it will work! according to Apple Doc
NSURLErrorRequestBodyStreamExhausted (-1021) Returns when the body stream is needed, but the client does not provide it. This affects iOS clients who send a POST request using the body stream but do not implement the NSURLConnection: needNewBodyStream delegate method connection.
so I needed to subclass AFHTTPRequestOperation and implement all delegate methods for NSURLConnection //. H
@interface CGHTTPRequestOperation : AFHTTPRequestOperation @end
// m.
@implementation CGHTTPRequestOperation #pragma mark NSURLConnection delegate methods - (NSInputStream *)connection:(NSURLConnection __unused *)connection needNewBodyStream:(NSURLRequest *)request { if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) { return [request.HTTPBodyStream copy]; } return nil; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [super connection:connection didReceiveResponse:response]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [super connection:connection didReceiveData:data]; } - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { [super connection:connection didSendBodyData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite]; } - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{ return [super connection:connection willCacheResponse:cachedResponse]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection{ [super connectionDidFinishLoading:connection]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [super connection:connection didFailWithError:error]; } - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection { return YES; } - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [super connection:connection willSendRequestForAuthenticationChallenge:challenge]; } @end
If you're curious how to use these extended classes to load data with multiple parts, here is an example //. H
typedef enum { CGFileUploadStatusError = 0, CGFileUploadStatusSuccess = 1, } CGFileUploadStatus; typedef void(^CGNetworkingFileUploadCBlock) (CGFileUploadStatus status,NSString *responseString);
// m.
+ (void) uploadImageAtPath:(NSString *) imagePath cBlock:(CGNetworkingFileUploadCBlock) cBlock { AFHTTPRequestSerializer *r = [AFHTTPRequestSerializer serializer]; NSDictionary *param = @{@"param1":@"",@"param2":@""}; NSData *d = [NSData dataWithContentsOfFile:imagePath]; __block NSString *paramNameForImage = [imagePath pathComponents].lastObject; NSError *error = nil; NSMutableURLRequest *urlRequest = [r multipartFormRequestWithMethod:@"POST" URLString:@"http://url_to_up_load_image" parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:d name:@"FileUploadControl" fileName:paramNameForImage mimeType:@"image/jpeg"]; } error:&error]; if (error) { NSLog(@"Some error:%@",error); } CGHTTPRequestOperation *requestOperation = [[CGHTTPRequestOperation alloc] initWithRequest:urlRequest];
Hope this helps those who suffer from this problem :)
Chamira Fernando Jan 23 '14 at 9:25 2014-01-23 09:25
source share