方法一:从url的response的header中提取
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"response%@",response);
NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dic=[httpResponse allHeaderFields];
NSLog(@"dic:%@",dic);
NSString *time=[dic objectForKey:@"Date"];
NSString *time1=[time substringFromIndex:16];
NSString *time2=[time1 substringToIndex:9];
NSArray*array=[time2 componentsSeparatedByString:@":"];
int hour=[[array objectAtIndex:0] intValue];
int minute=[[array objectAtIndex:1] intValue];
NSString *now=[NSString stringWithFormat:@"%0.2d:%0.2d",hour+8,minute];
NSLog(@"date:%@",now);
}
}
方法二:从下面的字段中提取
NSStringEncoding chineseEnc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *webString=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.time.ac.cn/timeflash.asp?user=flash"] encoding:chineseEnc error:nil];
NSLog(@"%@",webString);
webString====<?xml version='1.0' encoding='GB2312'?><ntsc><time><year>2013</year><month>12</month><day>18</day><Weekday></Weekday><hour>12</hour><minite>0</minite><second>12</second><Millisecond></Millisecond></time></ntsc>
从webString(xml文件格式)里面提取出时间就是了