0

我正在尝试为多部分形式上传的有效负载计算 content-md5 标头:

NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload"   
parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
  [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.png"  
mimeType:@"image/png"];
}];

我试图阅读HTTPBodyStream

if ([request HTTPBodyStream]) {
    NSInteger result;
    uint8_t buffer[1024]; 
    while((result = [[request HTTPBodyStream] read:buffer maxLength:1024]) != 0) 
    {
        if(result > 0) {         
            [data appendString:[NSString stringWithUTF8String:(char *)buffer]];

        } else {...}
    }

但我最终陷入了无限循环。有没有办法做到这一点?

更新

[[request HTTPBodyStream] streamError]如果我打破 while 循环 if ,则在循环null结束时返回,但是流将不完整。whileresult < 0

4

1 回答 1

0

在读取小于最大缓冲区大小(1024)的情况下使用 hasBytesAvailable && 怎么样。这意味着已达到 EOF。

`while([[request HTTPBodyStream] hasBytesAvailable] && [[request HTTPBodyStream] read:buffer maxLength:1024]) == 1024)'

于 2013-08-27T14:11:56.617 回答