NSURL/NSURLHandle and caching
NSURL/NSURLHandle and caching
- Subject: NSURL/NSURLHandle and caching
- From: Tom Waters <email@hidden>
- Date: Tue, 27 Nov 2001 19:22:35 -0800
i am struggling with NSURLHandle's propertyForKey: method.
i would expect that i could create an URL, get a handle to it, get the
resource data, then query the properties to see things like the return
codes, (for example 304 Not Modified and 404 Not Found).
if you use URLHandleUsingCache:YES this is indeed the behavior.
however, i need to NOT use the cache and make the request once each time.
the problem is, if I get an URLHandleUsingCache:NO, the subsequent
propertyForKey: call does ANOTHER HTTP request. this time with HEAD
instead of GET, but of course, that destroys the previous call's return
code... this defeating the purpose!
i even tried using the "UsingCache:YES" version and calling [handle
flushCachedData] afterwards, but it had no effect.
it's hard to describe what i'm trying to do, other than be a reasonable
http client... here's some code torn out of my app, which may or may not
make sense...
if you watch the server side of this (point it at say localhost), you
can see that the propertyForKey calls are causing NSURL to do a
subsequent HEAD call... if you change the NO to a YES, it does exactly
one GET, and will never do it again, no matter if i flushCachedData or
not.
- (void)get
{
NSURL *url = [NSURL URLWithString:@"
http://www.apple.com/index.html"];
NSURLHandle *handle = [url URLHandleUsingCache:NO];
NSData *data;
id lastmod, lastmodString;
id code, reason;
[handle writeProperty:@"MyApp/1.0" forKey:@"User-Agent"];
[handle writeProperty:strlastmod forKey:@"If-Modified-Since"];
data = [handle resourceData];
code = [handle propertyForKey:NSHTTPPropertyStatusCodeKey];
reason = [handle propertyForKey:NSHTTPPropertyStatusReasonKey];
NSLog(@"%@ %@ %@", rel_path, code, reason);
lastmodString = [handle propertyForKey:@"Last-Modified"];
[handle flushCachedData];
}
does anyone (at apple?) have an opinion as to if i am doing something
wrong, or i should just bail on NSURL/NSURLHandle and write my own
client side using SmallSockets?