A quick question. I have the following code which gets the mod date of a file on a server:
- (void) sendRequestForLastModifiedHeaders
{
/* send a request for file modification date */
NSURLRequest *modReq = [NSURLRequest requestWithURL:[NSURL URLWithString:URLInput.text]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[[NSURLConnection alloc] initWithRequest:modReq delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
/* convert response into an NSHTTPURLResponse,
call the allHeaderFields property, then get the
Last-Modified key.
*/
NSHTTPURLResponse *foo = [(NSHTTPURLResponse *)response allHeaderFields];
NSString * last_modified = [NSString stringWithFormat:@"%@",
[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Last-Modified"]];
NSLog(@"Last-Modified: %@", last_modified );
}
My main question is the following:
Does this call only send over the header? If the file is big I do not want the whole file being downloaded. That is why I'm checking the header.
+++++++++++++++++++++++++++++++++++++++ After the update this works... Thanks now looks like:
NSMutableURLRequest *modReq = [NSMutableURLRequest requ开发者_开发百科estWithURL:[NSURL URLWithString: URLInput.text]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];
[modReq setHTTPMethod:@"HEAD"];
As you have it now, you're probably downloading the whole file. The key is the http method used for the http request. By default, it's a GET request. What you want is a HEAD request. You don't want the file, you just want the server to return the response that you would get if you did, right?
To do that, you want to use a NSMutableURLRequest
and setHTTPMethod:
to construct a request with a method of HEAD, instead of GET.
精彩评论