NSMutableURLRequest 和“请求正文流已耗尽”错误
我对 http PUT 请求和请求正文作为来自文件的流有疑问。
无论文件大小是多少,我都会收到错误“NSURLErrorDomain -1021 request body stream exeded”
我知道我可以通过实现该方法来覆盖这个问题:
-(NSInputStream*)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
但这种方法不好,因为它会再次上传整个文件,并且 40 MB文件的数据传输量为 80 Mb。
如果我采用与 NSData 相同的文件并设置请求正文,则它可以正常工作。
我尝试发送请求异步并同步相同的结果。
这是我的代码,简单且类似于 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];
我做错了什么吗? 我错过了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,为我解决的问题是保留流,然后在异步 HTTP 事务结束时释放它。这似乎不是必要的,但它让我解决了这个问题。
I was encountering this same problem, what fixed it for me was retaining the stream then releasing it at the end of the asynchronous HTTP transaction. Doesn't seem like this should be necessary, however it got me around the problem.
从它的外观来看,您似乎没有在标头中设置@“Content-Length”。
我的做法是这样的:
无论哪种方式,我都在进行批量上传,偶尔会出现正文流耗尽错误。据我所知,问题是我的设备上只有很少的可用空间,并且在一些上传完成之前临时文件会被自动删除(当收到错误时,我测试检查文件是否仍然存在,并且事实并非如此)。
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:
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).