1
votes

I am downloading file (with apache tomcat 6.0.32). When I make disconnect (shutdown tomcat) some times ASHITTPRequest generate error, but sometimes (most of times exactly...ALL TIME EXACTLY! Only if there is no connection at the beginning occurs error) it ends work like all correct.

So there is the question: why this happened and how can I watch if the connection lost properly.

Thanks a lot!

UPDATE:

Try to send request through TCPMon and then stop it (TCPMon) and get the same: ASIHTTPRequest think that the downloading correctly done.

UPDATE:

responseHeaders:

"Content-Disposition" = attachment;
"Content-Length" = 2277888;
"Content-Type" = "application/octet-stream";
Date = "Thu, 28 Apr 2011 12:35:32 GMT";
Server = "Apache-Coyote/1.1";
"Set-Cookie" = "JSESSIONID=98CAE6C0C4275B528D5E0F8651546AFE; Path=/ISED";

responseStatusMessage:

 HTTP/1.1 200 OK

UPDATE:

If disconnect computer by hand (disconnect the cable) and get this:

Sometime ASIHTTPRequest waits till timeout, and next request get connecting error.

Sometime error occurs just in time I disconnect.

Sometime error does not occurs just in time I disconnect: download progress missing and (if make connection again) it file starts download from the beginning.

And if close port ASIHTTPRequest thinks that is successful download done.

I can't understand this logic...

SOLUTION:

I solve the problem by comparing Content-Length in header and total bytes read:

 NSString *contentLength = [[self.request responseHeaders] valueForKey:@"Content-Length"];
 NSString *downloadedBytesCount = [NSString stringWithFormat:@"%llu",[self.request totalBytesRead]];


        if ([contentLength isEqualToString:downloadedBytesCount] == NO) {
           // error maintain 
        } 

So I can check if I got all data and if not: maintain error. In other cases (like manually disconnect) ASIHTTPRequest work appropriate way and generates errors by self.

1
Have you tested this with NSURLConnection? What does the response(header and body) say in the requestFinished delegate method?Nick Weaver

1 Answers

1
votes

for checking connection status i am using Reachability library with ASI. i believe Reachability comes with ASI not sure.

this is not mine code i took from somewhere but it is working flawless.

#import "Reachability.h"

-(void)viewDidAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
}

- (void) checkNetworkStatus:(NSNotification *)notice
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            //self.internetActive = NO;

            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            //self.internetActive = YES;

            break;
        }
        case ReachableViaWWAN:
        {
           NSLog(@"The internet is working via WWAN.");
           //self.internetActive = YES;
           break;
        }
    }
}

i have altered the code little bit regarding my needs it returns YES/NO. and you should remove observer at some point.

- (void)viewDidUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

thanks.