Skip to content

Instantly share code, notes, and snippets.

@krhoyt
Created November 7, 2013 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krhoyt/7359548 to your computer and use it in GitHub Desktop.
Save krhoyt/7359548 to your computer and use it in GitHub Desktop.
Plain HTTP GET request.
- ( NSString * ) loadFileHTTPGet : ( NSString * ) url
{
NSArray * crayola;
NSArray * parts;
NSData * get;
NSError * error;
NSHTTPURLResponse * response;
NSMutableURLRequest * request;
NSString * contents;
// Load a file over HTTP GET
request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod : @"GET"];
[request setURL:[NSURL URLWithString : url]];
error = [[NSError alloc] init];
response = nil;
get = [NSURLConnection sendSynchronousRequest : request returningResponse : &response error : &error];
if( [response statusCode] != 200 )
{
NSLog( @"Error making HTTP request: %@", error );
} else {
contents = [[NSString alloc] initWithData : get encoding:NSUTF8StringEncoding];
NSLog( @"%@", contents );
// Parse basic CSV
crayola = [contents componentsSeparatedByString : @"\r"];
NSLog( @"%lu", (unsigned long)[crayola count] );
for( NSString * item in crayola )
{
parts = [item componentsSeparatedByString : @","];
NSLog( @"Name: %@, Hex: %@, Year: %@", [parts objectAtIndex : 0], [parts objectAtIndex : 1], [parts objectAtIndex : 6] );
}
}
return contents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment