【问题标题】:How can I implement chunked http requests on iPhone?如何在 iPhone 上实现分块的 http 请求?
【发布时间】:2010-11-05 18:35:35
【问题描述】:

目前我正在尝试使用 NSMutableURLRequest,将 HTTPBody 设置为我的自定义编写的 NSInputStream,以提供此块。

如果不是因为 NSMutableURLRequest 不断要求我在我的 NSInputStream 类中实现越来越多的方法,这会很好。首先它要求我实现 - streamStatus: 这很容易实现,但是现在它要求 _scheduleInCFRunLoop:forMode:...

基本上我是从算法中生成数据,并希望通过分块请求将其发送到服务器。

代码如下:

@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

【问题讨论】:

  • NSInputStream 实现了上述两种方法。你的流类真的是 NSInputStream 的子类吗?听起来好像你有来自 NSStream 而不是 NSInputStream 的子类。
  • 绝对继承自 NSInputStream。 -streamStatus: 方法已经存在,但是当调用它时说 NSStream 的实现是抽象的。另一个似乎没有实施。看起来 NSUrlRequest 除了一个非常具体的内部 NSInputStream。 :(
  • 我同样需要以编程方式创建输入流。事实证明这很困难。

标签: iphone cocoa-touch networking foundation http-chunked


【解决方案1】:

根据网上的一些讨论,很难继承NSInputStream。看看Multipart POST using NSInputStreamNSInputStream subclass asynchronous

看来你确实需要实现这些奇怪的私有方法……

【讨论】:

  • 难以置信,三年过去了,我们仍然不能继承 NSInputStream。我猜它只是不是苹果的优先事项?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多