如何在HTTP上将Last-Modified标头从HTTP响应正确转换为NSDate [英] How to properly convert the Last-Modified header from an HTTP response to an NSDate on iOS

查看:162
本文介绍了如何在HTTP上将Last-Modified标头从HTTP响应正确转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个完全有效的解决方案,一个适用于:

I am looking for a fully working solution, one that works with:


  • 任何iOS区域设置或时区

  • 任何/大多数HTTP服务器

  • Xcode 4.0.2 (查看原因)

  • any iOS locale or timezone
  • any/most HTTP servers
  • Xcode 4.0.2 (see why)

当前破碎的代码:

 NSString lastModifiedString = @"Mon, 06 Jun 2011 12:47:05 GMT";
 NSDateFormatter *df = [[NSDateFormatter alloc] init];  
 //df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
 df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
 df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];  
 df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
 NSDate date = [df dateFromString:lastModifiedString];  

我认为假设 Last-Modified 使用与HTTP规范中的其他日期字段相同的格式,意味着使用RFC-1123 / RFC-822

I assumed that Last-Modified is supposed to use the same format like other date fields in HTTP spec, meaning to use RFC-1123 / RFC-822

资源:

  • http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html HTTP 1.1
  • http://developer.apple.com/library/ios/#qa/qa1480/_index.html

推荐答案

唯一不对的是你错过了星号 NSString lastModifiedString ,以及 NSDate 日期之间。一旦你把它们放进去,这段代码就适合我。

The only thing wrong is that you're missing asterisks in-between NSString and lastModifiedString, and also between NSDate and date. Once you put those in, this code works for me.

这篇关于如何在HTTP上将Last-Modified标头从HTTP响应正确转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆