Skip to content

Instantly share code, notes, and snippets.

@Racum
Created September 10, 2012 01:54
Show Gist options
  • Save Racum/3688400 to your computer and use it in GitHub Desktop.
Save Racum/3688400 to your computer and use it in GitHub Desktop.
NSMutableURLRequest and NSURLConnectionDelegate
- (void)go {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://httpbin.org/headers"]];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:@"GET"];
[request setValue:@"Test" forHTTPHeaderField:@"User-Agent"];
[request setTimeoutInterval:10];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
NSLog(@"Connecting...");
} else {
NSLog(@"Error!");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"%@", response);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"%@",[error localizedDescription]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment