NSMutableURLRequest和“请求主体流耗尽”错误 [英] NSMutableURLRequest and "request body stream exhausted" error

查看:296
本文介绍了NSMutableURLRequest和“请求主体流耗尽”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题与http PUT请求和请求正文作为流文件。



无论文件的大小,我得到错误NSURLErrorDomain -1021请求body stream exhausted



我知道我可以通过实现方法重写这个问题:
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream :( NSURLRequest *)request



但是这种方法不好,因为它会再次上传整个文件,而40 MB的文件是80 Mb的数据传输。 / p>

如果我采用与NSData相同的文件并设置请求主体它工作正常。



请求异步并同步同步两个结果。



这里是我的代码,简单和类似于来自苹果的示例:

  NSURL * url = [NSURL URLWithString:[self concatenatedURLWithPath:path]]; 
NSMutableURLRequest * req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@PUT];
[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:DEFAULT_TIMEOUT];
[req setValue:_contentType forHTTPHeaderField:@Content-Type];
NSInputStream * fileStream = [NSInputStream inputStreamWithFileAtPath:_dataStreamLocation];

[req setHTTPBodyStream:fileStream];
_connection = [[NSURLConnection connectionWithRequest:req delegate:self] retain];

我做错了什么?
我错过了什么?



谢谢!



Eitan

解决方案

从它的外观,你似乎没有在标题中设置@Content-Length。



我这样做的方式是这样:

  NSUInteger fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath :_dataStreamLocation error:nil] objectForKey:NSFileSize] unsignedIntegerValue]; 
[request setValue:[NSString stringWithFormat:@%u,fileSize] forHTTPHeaderField:@Content-Length];

无论哪种方式,我正在做批量上传,偶尔会得到body stream exhaustion错误。从我可以告诉,问题是,我只有很少的设备上的可用空间,临时文件将在一些上传完成之前自动删除(当接收到我测试的错误,以检查文件是否仍然存在,和它不是)。


I have a problem with http PUT request and request body as stream from file.

No matter what the size of the file i get error "NSURLErrorDomain -1021 request body stream exhausted"

I know i can override this problem by implementing the method: -(NSInputStream*)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request

but this approach is not good as it will upload the whole file again, and 40 MB of file turns out to be 80 Mb of data transfer.

if i take the same file as NSData and set the request body it works fine.

I tried sending the request Async and sync same result in both.

Here is my code, simple and similar to example from Apple:

NSURL *url = [NSURL URLWithString:[self concatenatedURLWithPath:path]];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"PUT"];
[req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:DEFAULT_TIMEOUT];
[req setValue:_contentType forHTTPHeaderField:@"Content-Type"];
NSInputStream *fileStream = [NSInputStream inputStreamWithFileAtPath:_dataStreamLocation];

[req setHTTPBodyStream:fileStream];
_connection = [[NSURLConnection connectionWithRequest:req delegate:self] retain];

Am i doing something wrong? Am i missing something?

Thanks!

Eitan

解决方案

From the looks of it, it seems that you're not setting @"Content-Length" in the header.

The way I do it is like this:

NSUInteger fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:_dataStreamLocation error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
[request setValue:[NSString stringWithFormat:@"%u", fileSize] forHTTPHeaderField:@"Content-Length"];

Either way, I was doing batch uploading and occasionally got the body stream exhaustion error. From what I could tell, the issue was that I only had few free space on the device, and the temporary files would get deleted automatically before some uploads being finished (when receiving the error I tested to check if the file was still there, and it wasn't).

这篇关于NSMutableURLRequest和“请求主体流耗尽”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆