Question

I am facing a weird problem in downloading xml files from server.

I am trying to get schedulist.xml using the following code,

- (void) downloadXml:(NSString*)url {

_urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

    [_urlRequest setHTTPMethod:@"GET"];

       if(_urlConnection !=NULL)
        return NO;

    _urlConnection = [[NSURLConnection alloc] initWithRequest:_urlRequest delegate:self startImmediately:YES];

}

The problem is ,

  1. When internet is available , If I click the button to download xml,Fine. Xml is getting downloaded.
  2. Turned off the internet now, If I click the button again, it is calling didReceiveResponse,didReceiveData and connectionDidFinishLoading. FYI , I have deleted the old response data.

But the thing is , If I use [_urlRequest setHTTPMethod:@"POST"]; It is working fine, which is

  1. Working fine with internet
  2. didFailWithError is getting called

What could be the problem ?

Was it helpful?

Solution

By default NSURLRequest uses protocol data to decide whether to cache request or not.
For example, if "Cache-control" or "Expires" directives are present in response headers (RFC2616), their values will be used to decide if cached response can be used as it is.
Usually only GET requests are cached, that's why you get didFailWithError on your POST request.
To avoid caching you can fix headers on your server or set cachePolicy property of NSURLRequest to NSURLRequestReloadIgnoringLocalCacheData

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top