Created
August 5, 2011 12:57
-
-
Save iosdeveloper/1127486 to your computer and use it in GitHub Desktop.
NSDictionary category which sends a HEAD request and returns the header fields as dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSDictionary (MBDictionary) | |
+ (id)dictionaryWithHeadOfURL:(NSURL *)inURL; | |
@end | |
@implementation NSDictionary (MBDictionary) | |
+ (id)dictionaryWithHeadOfURL:(NSURL *)inURL { | |
NSDictionary *theResult = nil; | |
if ([@"http" isEqualToString:[inURL scheme]] || [@"https" isEqualToString:[inURL scheme]]) { | |
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:inURL]; | |
NSURLResponse *theResponse = nil; | |
NSError *theError = nil; | |
[theRequest setHTTPMethod:@"HEAD"]; | |
[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError]; | |
if (theError == nil && [theResponse respondsToSelector:@selector(allHeaderFields)]) { | |
theResult = [(id)theResponse allHeaderFields]; | |
} | |
} | |
return theResult; | |
} | |
@end | |
Usage: | |
NSDictionary *theHeader = [NSDictionary dictionaryWithHeadOfURL:[NSURL URLWithString:(NSString *)URLString]]; | |
NSLog(@"%@", theHeader); | |
Result (example): | |
{ | |
"Accept-Ranges" = bytes; | |
Connection = "Keep-Alive"; | |
"Content-Length" = 5342; | |
"Content-Type" = "text/plain"; | |
Date = "Fri, 05 Aug 2011 12:59:00 GMT"; | |
Etag = "\"9aaac21-14de-4a98c147854d7\""; | |
"Keep-Alive" = "timeout=3, max=100"; | |
"Last-Modified" = "Tue, 02 Aug 2011 21:03:30 GMT"; | |
Server = "Apache/2.2.19 (Unix) mod_ssl/2.2.19 OpenSSL/0.9.8r"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment