0
votes

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?

1
What happens when you try the code you posted? Have you tried stepping into the [request clearDelegatesAndCancel] to figure out why it's not stopping the download?Malcolm Box

1 Answers

0
votes

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 :(