方法一:
//获取网络当前时间
- (NSDate *)getInternetDate {
NSString *urlString = @"http://m.baidu.com";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
// 实例化NSMutableURLRequest,并进行参数配置
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval: 2];
[request setHTTPShouldHandleCookies:FALSE];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 处理返回的数据
//NSLog(@"response is %@",response);
NSString *date = [[response allHeaderFields] objectForKey:@"Date"];
date = [date substringFromIndex:5];
date = [date substringToIndex:[date length]-4];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
NSDate *netDate = [[formatter dateFromString:date] dateByAddingTimeInterval:60*60*8];
return netDate;
}
方法二:
NSURL *url=[NSURL URLWithString:@"http://m.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 *dict=[httpResponse allHeaderFields];
// NSLog(@"dict--%@",dict);
NSString *date=[dict objectForKey:@"Date"];
NSLog(@"date--%@___class---%@",date,[date class]);
}}