AFNetworking中的Calc Content-MD5

时间:2013-08-27 08:45:34

标签: ios objective-c http md5 afnetworking

我正在尝试计算多部分表单上传的有效负载的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循环,则流将不完整。

1 个答案:

答案 0 :(得分:0)

使用hasBytesAvailable&amp;&amp;如果读数小于最大缓冲区大小(1024)。这意味着已达到EOF。

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