简体   繁体   English

写入大型HTTP响应文件的最佳方法

[英]Best way to write to file a large HTTP response

I'm looking for ways of writing to file the results of a web request. 我正在寻找写入Web请求结果的方法。 In languages based on the JVM or the CLR there are appropriate Stream-based techniques I'm familiar with, however I'm clueless on how could that be done in Objective-C. 在基于JVM或CLR的语言中,有一些我熟悉的适当的基于流的技术,但是我对在Objective-C中如何做到这一点一无所知。 What I need is essentially a way to send an HTTP request (with a custom header set) and write the HTTP response content as I receive it (due to memory constraints I can't afford to get the whole file or even a large portion of it before persisting the contents). 实际上,我需要的是一种发送HTTP请求(带有自定义标头集)并在收到请求时写入HTTP响应内容的方法(由于内存限制,我无力获取整个文件或什至很大一部分)在保留内容之前)。

Ideas/suggestions/snippets? 想法/建议/摘要?

Thanks in advance! 提前致谢!

PS: I'm developing for Mac OS and I'm already using ASIHTTPRequest, if that can be of help. PS:我正在为Mac OS进行开发,如果可以提供帮助,我已经在使用ASIHTTPRequest。

Edit: I should specify that I don't want to write all of the contents returned by the server to disk unless I can write them directly at a certain offset of a file (which I'll then be able to manipulate), so anything that dumps straight to a new file or to the beginning of a file won't work for me. 编辑:我应该指定我不希望将服务器返回的所有内容都写到磁盘上,除非我可以直接在文件的某个偏移量处写入它们(然后便可以操作),因此直接转储到新文件或文件开头的文件对我不起作用。

There a few ways of doing it, depends on how you want to handle the responds 有几种方法,取决于您要如何处理响应

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/Test/Desktop/cool.html"];

with setDownloadDestinationPath: set, it'll save into temporary path, and when it finished, it'll move it to your downloadDestinationPath you set. 使用setDownloadDestinationPath: set时,它将保存到临时路径中,完成后会将其移动到您设置的downloadDestinationPath

Or you can implement request:didReceiveData: delegate (see ASIHTTPRequestDelegate.h), and handle it yourself. 或者,您可以实现request:didReceiveData: didReceiveData request:didReceiveData:委托(请参阅ASIHTTPRequestDelegate.h),然后自己处理。 This is similar to stream. 这类似于流。

PS. PS。 I only ever use ASIHTTPRequest on iOS, not Mac OS, so I'm not entirely sure if it will work for you. 我只在iOS而不是Mac OS上使用ASIHTTPRequest,所以我不确定它是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM