如何取消响应 HTTP 代码错误的 ASIHTTPRequest?

发布于 2024-12-06 02:51:11 字数 909 浏览 1 评论 0原文

我正在使用 ASIHTTPRequest 进行 HTTP 请求并将结果保存到单独的文件中。由于文件预计会很大,因此它也应该能够恢复。

request.allowResumeForFileDownloads = YES;
request.downloadDestinationPath = destFile;
request.temporaryFileDownloadPath = tmpFile;

我使用以下选择器:

request.didFinishSelector = @selector(didFinishRequest:);
request.didFailSelector = @selector(didFailRequest:);
request.didReceiveResponseHeadersSelector = @selector(didReceiveResponse:);

现在我正在测试连接丢失、不同代理等的不同情况,并发现一个问题,如果收到诸如 503 之类的代码的响应,则 ASIHTTPRequest 仍然会将结果保存到临时文件中,该文件是错误的,因为数据是一些有错误的随机 HTML 页面(如果有部分下载的文件,则此 HTML 会附加到其中,这会使我的数据损坏)。

所以我想做的是检查响应代码,如果不是 2XX,则取消请求而不保存任何内容。

这没有帮助:

- (void)didReceiveResponse:(ASIHTTPRequest*)request {
    if (request.responseStatusCode < 200 || request.responseStatusCode >= 300) {
        [request clearDelegatesAndCancel];

有什么想法吗?

I'm uisng ASIHTTPRequest for HTTP requests and to save result into separate file. As the file is expected to be large, it should also be able to resume.

request.allowResumeForFileDownloads = YES;
request.downloadDestinationPath = destFile;
request.temporaryFileDownloadPath = tmpFile;

I use the following selectors:

request.didFinishSelector = @selector(didFinishRequest:);
request.didFailSelector = @selector(didFailRequest:);
request.didReceiveResponseHeadersSelector = @selector(didReceiveResponse:);

Now I'm testing different cases with loosing connection, different proxies, etc and found a problem, that if a response is received with code other like 503 or something, then ASIHTTPRequest still saves result into temp file, which is wrong, because the data is some random HTML page with error (if there was partial downloaded file, then this HTML is appended to it which makes my data corrupted).

So what I'm trying to do is to check response code and if it is not 2XX, then cancel request without saving anything.

This doesn't help:

- (void)didReceiveResponse:(ASIHTTPRequest*)request {
    if (request.responseStatusCode < 200 || request.responseStatusCode >= 300) {
        [request clearDelegatesAndCancel];

Any ideas?

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

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

发布评论

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

评论(1

等你爱我 2024-12-13 02:51:11

到目前为止还没有找到一个干净的解决方案,所以有一个小技巧:

在 ASIHTTPRequest.m 方法 readResponseHeaders 的最后一行中,我们需要更改为 waitUntilDone:YES (第 2281 行),因此代码不会继续运行和处理数据。

    [self performSelectorOnMainThread:@selector(requestReceivedResponseHeaders:) withObject:[[[self responseHeaders] copy] autorelease] waitUntilDone:YES];

不幸的是,在 didReceiveResponse: 选择器中,我们不能简单地调用clearDelegatesAndCancel 方法,因为由于 ASIHTTPRequest 中的锁,它只会挂起线程。所以我所做的只是使用了我从 ASIHTTPRequest 中获取的一些方法:

    [request setComplete:YES];
    [request setDownloadComplete:YES];
    [request requestFinished];
    [request markAsFinished];

虽然前两个方法不是公开的并且 Xcode 显示警告,但它仍然有效。

顺便说一句,就在两天前,我看到一个 post 说 ASIHTTPRequest 不再受支持:(

So far haven't found a clean solution, so there is a little hack:

In ASIHTTPRequest.m in the very last line of method readResponseHeaders we need to change to waitUntilDone:YES (line 2281), so the code doesn't continue running and processing data.

    [self performSelectorOnMainThread:@selector(requestReceivedResponseHeaders:) withObject:[[[self responseHeaders] copy] autorelease] waitUntilDone:YES];

Unfortunately, in the didReceiveResponse: selector we can't simply call clearDelegatesAndCancel method, as it will just hang the thread, because of the locks in ASIHTTPRequest. So what I did is just used some of the methods that I took from ASIHTTPRequest:

    [request setComplete:YES];
    [request setDownloadComplete:YES];
    [request requestFinished];
    [request markAsFinished];

Although, the first two methods are not public and Xcode shows warning, but it still works.

By the way, just 2 days ago I saw a post saying that ASIHTTPRequest is not being supported any more :(

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