Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Get Header Results

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 requestWithURL:[NSURL URLWithString:               URLInput.text]
                                        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0f];   
[modReq setHTTPMethod:@"HEAD"];
like image 364
rsirota Avatar asked Aug 31 '11 00:08

rsirota


People also ask

How do I find a header value?

get() The get() method of the Headers interface returns a byte string of all the values of a header within a Headers object with a given name. If the requested header doesn't exist in the Headers object, it returns null .

Can Javascript read headers?

While you can't ready any headers of HTML response in JS, you can read Server-Timing header, and you can pass arbitrary key-value data through it.


1 Answers

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.

like image 81
Mike Hay Avatar answered Oct 29 '22 18:10

Mike Hay