ios下表单post使用gzip模式
Posted 自由出土文物
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios下表单post使用gzip模式相关的知识,希望对你有一定的参考价值。
使用afnetworking,服务器参考的这里
ios端,使用自己的序列化类
manager.requestSerializer = [MyHttpRequestSerializer new];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[manager.requestSerializer setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
h文件
#import <Foundation/Foundation.h> #import "AFURLRequestSerialization.h" @interface MyHttpRequestSerializer : AFHTTPRequestSerializer @end
关键代码,参考AFJSONRequestSerializer
@implementation MyHttpRequestSerializer - (nullable NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request withParameters:(nullable id)parameters error:(NSError * _Nullable __autoreleasing *)error NS_SWIFT_NOTHROW{ NSParameterAssert(request); if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) { return [super requestBySerializingRequest:request withParameters:parameters error:error]; } NSMutableURLRequest *mutableRequest = [request mutableCopy]; [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) { if (![request valueForHTTPHeaderField:field]) { [mutableRequest setValue:value forHTTPHeaderField:field]; } }]; if (parameters) { if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) { [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; } NSData* data = [NSJSONSerialization dataWithJSONObject:parameters options:(NSJSONWritingOptions)0 error:nil]; NSData* zippeddata = [LFCGzipUtillity gzipData:data]; [mutableRequest setHTTPBody:zippeddata]; } return mutableRequest; } @end
以上是关于ios下表单post使用gzip模式的主要内容,如果未能解决你的问题,请参考以下文章
csharp C#代码片段 - 使类成为Singleton模式。 (C#4.0+)https://heiswayi.github.io/2016/simple-singleton-pattern-us