在使用 Safari 的时候,我们会注意到一个很常见的乱码问题,如下图:
这是在打开 http://att.newsmth.net/att.php?p.719.214628.536.png 这样的图片链接时,Safari 错误的判断了这个图片文件的文件名造成的。而为什么会有这样的错误判断呢?
其实 Safari 使用的是 Cocoa 框架 [URL Loading](http://developer.apple.com/documentation/ Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html) 架构中的 NSURLResponse 类的 suggestedFilename 方法实现的。
而这个方法,其实就是解析 HTTP 首部中的 Content-Disposition 域里的 filename 部分完成的,比如下面这个首部:
$ curl -I http://att.newsmth.net/att.php?p.719.214628.536.png HTTP/1.1 200 OK …. Content-Disposition: inline;filename=ͼƬ_6.png ….
显然这是乱码,可奇怪的是,这和我们在上面的图中看到的乱码又不一样,这是为什么呢?
假如将它作为 GBK 来解码就清楚了:
$ curl -I http://att.newsmth.net/att.php?p.719.214628.536.png | iconv -f gbk -t utf-8 HTTP/1.1 200 OK …. Content-Disposition: inline;filename=图片_6.png ….
哦,原来是 GBK 编码的“图片_6.png”,可是这个文件名怎么会变成开头图片中那种形式的乱码呢?其实写一段 Cocoa 程序就可以发现:
#import <Foundation/Foundation.h>
int main() { const char [...]
© jjgod / blog. Powered by WordPress using the DePo Skinny Theme.