码迷,mamicode.com
首页 > 移动开发 > 详细

ios下表单post使用gzip模式

时间:2018-05-10 17:10:42      阅读:481      评论:0      收藏:0      [点我收藏+]

标签:with   tor   methods   uppercase   tar   obj   class   sts   cas   

使用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模式

标签:with   tor   methods   uppercase   tar   obj   class   sts   cas   

原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/9019962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!