I’m new to Objective-c. I’m trying to do a simple get request and get the page content as a result but the response is always empty.
Here is my code, what am I doing wrong here?
main
HttpManager* pHttpManager;
pHttpManager = [[HttpManager alloc] init];
NSURL *myURL = [pHttpManager getUrlContent:@"http://www.wikipedia.org"];
NSError* error = nil;
if(myURL) {
NSLog(@"NSURL is valid");
NSLog(@"absoluteString:%@",myURL.absoluteString);
NSString *myHomePage = [NSString stringWithContentsOfURL: myURL encoding: NSASCIIStringEncoding error:&error];
if(myHomePage) {
NSLog(@"%@", myHomePage);
} else {
NSLog(@"Error = %@", error);
}
} else {
NSLog(@"NSURL with error");
}
HttpManager Class
@implementation HttpManager
-(NSURL*) getUrlContent:(NSString*) url {
NSURL *myURL = [NSURL URLWithString:url];
return myURL;
}
The Error:
$ ./obj/simple_app.exe
2013-01-13 10:15:06.400 simple_app[7656] NSURL is valid
2013-01-13 10:15:06.406 simple_app[7656] absoluteString:http://www.wikipedia.org
2013-01-13 10:15:07.383 simple_app[7656] unable to set blocking mode – An invalid argument was supplied.
2013-01-13 10:15:07.398 simple_app[7656] Error = NSCocoaErrorDomain 259
Normally one would do this:
What happens when you try that? (And why did you use
NSASCIIStringEncodingin the first place?)The web page at
http://www.wikipedia.orgcontains Unicode characters that cannot be represented in the ASCII encoding, but can be represented in the (far more modern) UTF-8 encoding. There is rarely any reason to use ASCII anymore unless you are working with very old data.