Created
September 10, 2012 01:54
-
-
Save Racum/3688400 to your computer and use it in GitHub Desktop.
NSMutableURLRequest and NSURLConnectionDelegate
This file contains 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
- (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