yeluolei
yeluolei

Reputation: 83

Objective C get html from URL, encoding wrong

I want to ge the html content from the url bellow:

https://bbs.sjtu.edu.cn/file/bbs/mobile/top100.html

The code I have use below:

NSURL *url2 = [NSURL URLWithString:@"http://bbs.sjtu.edu.cn/file/bbs/mobile/top100.html"];
NSString *res = [NSString stringWithContentsOfURL:url2 encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:nil];
NSLog(@"%@",res);

the result is null. I have also tried with UTF8 encoding (also null) and ASCII encoding (has content and the english part of the content is right, but for Chinese charset, the content is garbled).

any one can help about this problem? I have been stuck here for a lot of time.

Upvotes: 0

Views: 2033

Answers (1)

Jay
Jay

Reputation: 6638

Try using stringWithContentsOfURL:usedEncoding:error: instead:

NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url2
                                                 usedEncoding:&encoding 
                                                        error:&error];

Upvotes: 2

Related Questions