我正在尝试计算多部分表单上传的有效负载的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]
在null
循环结束时返回while
,如果我在result < 0
处断开while循环,则流将不完整。
答案 0 :(得分:0)
使用hasBytesAvailable&amp;&amp;如果读数小于最大缓冲区大小(1024)。这意味着已达到EOF。
`while([[request HTTPBodyStream] hasBytesAvailable]&amp;&amp; [[request HTTPBodyStream] read:buffer maxLength:1024])== 1024)'