使用 ASIHTTPRequest 请求图像期间何时收到响应代码?

发布于 2024-11-07 08:02:13 字数 1099 浏览 0 评论 0原文

我什么时候才能真正收到有效图像请求的响应代码 200?是在所有数据都下载到我的浏览器之后还是哪个设备正在请求图像?

我正在使用库 http://allseeing-i.com/ASIHTTPRequest/ 下载我的图像iPad 应用程序并使用直接下载到文件选项,然后在出现 404 错误或 200 以外的任何其他状态代码时删除该文件。

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.jpg"];

问题是部分响应似乎在慢速连接期间被保存,因此我最终得到空白或损坏的结果图像。

相反,我决定仅在收到状态代码 200 后才将数据流保存到磁盘:

NSURL *url = [NSURL URLWithString: [[NSString stringWithFormat:kProductImagesURL, fileName] stringByAppendingString:tStamp]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTimeOutSeconds:10];
[request startSynchronous];

int statusCode = [request responseStatusCode];
 if (statusCode==200) {
     NSData *responseData = [request responseData];
     [responseData writeToFile:[savePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "] atomically:YES];
    }

我只想确保响应代码仅在请求完成且所有数据均已下载后返回。我 99% 确信情况确实如此,但无法承担另一个包含此类图像错误的应用程序版本。

When do I actually get my response code 200 for a valid request for an image? Is it after all of the data has been downloaded to my browser or which ever device is requesting the image?

I am using the library http://allseeing-i.com/ASIHTTPRequest/ to download images in my iPad app and using the download directly to a file option and then deleting the file if it was a 404 error or any other status code than 200.

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:@"/Users/ben/Desktop/my_file.jpg"];

Problem is partial responses seem to be getting saved during slow connections so I end up with blank or corrupt images.

I decided instead to save the data stream to disk only after I received a status code of 200:

NSURL *url = [NSURL URLWithString: [[NSString stringWithFormat:kProductImagesURL, fileName] stringByAppendingString:tStamp]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setTimeOutSeconds:10];
[request startSynchronous];

int statusCode = [request responseStatusCode];
 if (statusCode==200) {
     NSData *responseData = [request responseData];
     [responseData writeToFile:[savePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "] atomically:YES];
    }

I just want to make sure that the response code only comes back after the request has been completed and all of the data has been downloaded. I am 99% sure that is the case but can't afford another app release with an image bug like this in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作业与我同在 2024-11-14 08:02:13

您可能应该考虑切换到异步请求有两个原因。首先,它释放了你的主线程来与用户交互(即使是模态旋转器也很好,否则看起来你的应用程序已经冻结了)。

其次,它为您提供仅在整个请求完成后才会发生的回调。我无法真正解释仅使用您展示的代码获取部分数据,但我使用 ASI 的异步方法从未遇到过这个问题。

There are two reasons you should probably consider switching to an asynchronous request. The first is, it frees up your main thread to interact with the user (even a modal spinner would be nice--otherwise it looks like your app has frozen).

Second, it gives you callbacks that only happen once the whole request is finished. I can't really explain only having gotten partial data with the code you showed, but I've never once had that problem using ASI's asynchronous methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文