NSURLConnection/NSURLRequest gzip support

假装没事ソ 提交于 2019-11-26 19:23:37

问题


Does anyone knows if NSURLConnection/NSURLRequest have support for gzip requests.

If does, can you provide more information?


回答1:


although it does not seem to be documented, there is evidence that NSURLConnection does have transparent gzip support. meaning that if the server supports gzip encoding, and your request has an Accept-Encoding header containing gzip*, the server will send a gzipped response, which NSURLConnection will automatically decode.

* NSURLRequest might add that header by default. if not, you have to add it manually like so:

 [urlReq setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]



回答2:


NSURLRequest decodes gzip to NSData; such as the Server response contain "Content-Encoding" = gzip; the NSData will decode. If you don't want to automatically decode it, add the code blow. This is a private API.

//import CFNetwork.framework
extern CFStringRef kCFURLRequestDoNotDecodeData;
typedef const struct _CFURLRequest* CFURLRequestRef;
extern void _CFURLRequestSetProtocolProperty(CFURLRequestRef,CFStringRef,CFTypeRef);

//NSURLRequest init ...
//...
CFURLRequestRef requestRef = (CFURLRequestRef)[request performSelector:@selector(_CFURLRequest)];
_CFURLRequestSetProtocolProperty(requestRef,kCFURLRequestDoNotDecodeData,kCFBooleanTrue);


来源:https://stackoverflow.com/questions/2682483/nsurlconnection-nsurlrequest-gzip-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!