开发者

How can I implement chunked http requests on iPhone?

开发者 https://www.devze.com 2023-01-23 12:21 出处:网络
Currently I\'m try开发者_运维百科ing to use a NSMutableURLRequest, setting the HTTPBody to a custom written NSInputStream of mine that provides this chunks.

Currently I'm try开发者_运维百科ing to use a NSMutableURLRequest, setting the HTTPBody to a custom written NSInputStream of mine that provides this chunks.

This would be fine if it were not for the fact that NSMutableURLRequest keeps asking me to implement more and more methods in my NSInputStream class. First it asked me to implement - streamStatus: and that was rather straightforward to implement however now it's asking for _scheduleInCFRunLoop:forMode:...

Basically I'm generating data out of an algorithm and would like to send it to the server via chunked request.

Here's the code:

@interface GERHTTPChunkInputStream : NSInputStream
{
  uint8_t counter_;
}

- (GERHTTPChunkInputStream *)init;
- (void)dealloc;
- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len;
- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len;
- (BOOL)hasBytesAvailable;
- (NSStreamStatus)streamStatus;

@end

@implementation GERHTTPChunkInputStream

- (GERHTTPChunkInputStream *)init {
  [super init];
  return self;
}

- (void)dealloc {
  assert(NO);
  [super dealloc];
}

- (NSInteger)read:(uint8_t *)buffer maxLength:(NSUInteger)len {
  NSLog(@"Getting more bytes!!!");
  for (int i = 0; i < len; ++i) {
    buffer[i] = ++counter_;
  }
  return len;
}

- (BOOL)getBuffer:(uint8_t **)buffer length:(NSUInteger *)len {
  return NO;
}

- (BOOL)hasBytesAvailable {
  return YES;
}

- (NSStreamStatus)streamStatus {
  return NSStreamStatusNotOpen;
}

@end


According to a few discussions on the net, it is difficult to subclass NSInputStream. Have a look at Multipart POST using NSInputStream or NSInputStream subclass asynchronous.

It seems that you indeed need to implement these strange private methods...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号