简体   繁体   中英

NSData return nil when parse html

I want to get some information from this website " http://phim14.net/ ". I use this code :

NSURL *url = [NSURL URLWithString:@"http://phim14.net/"];
     NSError *error;
     NSData *htmlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
     TFHpple *parser = [TFHpple hppleWithHTMLData:htmlData];

At the beginning this code is working and I can get some info. But after several times trying I met an error that "htmlData" was return nil, so I think the website was down but I can connect to that webpage using Firefox. I try to parse other website but it's still ok? I try to print out the error and it said that

`"error NSError *   domain: @"NSCocoaErrorDomain" - code: 256   0x00007fbb224023c0"`

Someone please help me figure out what is the problem??

It seems errorcode 256 doesn't have much description. So for safe way to get data from URL , use this method sendSynchronousRequest:returningResponse:error: from NSURLConnection .

NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] returningResponse:nil error:&error];

Also please check these answer1 , answer2

Your URL is missing the scheme: " http:// " and this is giving you the error:

Error -> Error Domain=NSCocoaErrorDomain Code=256

Try adding this to the URL and it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM