Skip to content

Instantly share code, notes, and snippets.

@0x8badf00d
Created February 1, 2012 11:37
Show Gist options
  • Save 0x8badf00d/1716675 to your computer and use it in GitHub Desktop.
Save 0x8badf00d/1716675 to your computer and use it in GitHub Desktop.
NSURLConnection
-(void) downloadAndParse
{
NSURL *nsurl = [[[NSURL alloc] initWithString:@"http://10.230.148.253:8080/IDP/services/login/authuser"] autorelease];
NSMutableURLRequest* urlRequest = [[[NSMutableURLRequest alloc] initWithURL:nsurl] autorelease];
NSURLConnection* conn = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
if(conn)
{
// Declare an iVar NSMutableData* responseData
responseData = [[NSMutableData alloc] init];
}
}
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
NSInteger statusCode = [httpResponse statusCode];
NSLog(@"Http Status Code: %i",statusCode);
if ([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
if (statusCode >= 400)
{
[connection cancel]; // stop connecting; no more delegate messages
NSDictionary *errorInfo
= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:
NSLocalizedString(@"Server returned status code %d",@""),
statusCode]
forKey:NSLocalizedDescriptionKey];
NSError *statusError
= [NSError errorWithDomain:NSHTTPPropertyStatusCodeKey
code:statusCode
userInfo:errorInfo];
[self connection:connection didFailWithError:statusError];
}
}
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
connection = nil;
[responseData release],responseData = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *result =[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"downloaded idp output %@",result);
[result release];
[responseData release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment