使用AFN 的POST请求出现错误"Error Domain=NSCocoaErrorDomain Code=3840 "决绝方案

使用最新版本的AFN 进行HTTP的网络请求偶尔遇到的问题, 有时候自己就解决了, 也不知道原因, 后来 stackOverFlow上看到一个答案试了一下 可以解决 不过他的都是用C 写的, 简单改了一下 还是使用AFN

代码

/* 主要就是给 请求的过程添加一个安全政策的对象 并使用 */
+ (void)POSTWithURL:(NSString *)url withBody:(id)body withBodyStyle:(bodyStyle)bodyStyle withHttpHead:(NSDictionary *)head responseStyle:(responseStyle)style withSuccess:(void (^)(id))success withFail:(void (^)(NSError *))fail {

    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];

    NSString *url_string = [NSString stringWithString:url];
    /* 创建http请求管理者 */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    /* 处理body类型 */
    switch (bodyStyle) {
        case stringStyle: {
            break;
        }
        case JSONStyle: {
            break;
        }
    }

    /* 添加请求头 */
    if (head) {
        for (NSString *key in head) {
            [manager.requestSerializer setValue:head[key] forHTTPHeaderField:key];
        }
    }
    /* 判断返回数据类型 */
    switch (style) {
        case DATA: {
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        }
        case JSON: {
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        }
        case XML: {
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        }

        default:
            break;
    }
    /* 设置请求接受的数据类型 */
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

//    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil]];
    //本地缓存设置,沙盒路径设置
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *pathString = path.lastObject;
    NSString *pathLast =[NSString stringWithFormat:@"/Caches/com.hackemist.post.default/%lu.text", (unsigned long)[url_string hash]];

    //创建字符串文件存储路径
    NSString *PathName =[pathString stringByAppendingString:pathLast];

    //第一次进入判断有没有文件夹,如果没有就创建一个
    NSString * textPath = [pathString stringByAppendingFormat:@"/Caches/com.hackemist.post.default"];
    if (![[NSFileManager defaultManager]fileExistsAtPath:textPath]) {

        [[NSFileManager defaultManager]createDirectoryAtPath:textPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    //设BOOL值 判断解析后的数据是数组还是字典
    __block  BOOL isClass = NO;


    /* POST请求 */
    [manager POST:url parameters:body constructingBodyWithBlock:^(id  _Nonnull formData) {

    } progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSArray class]]) {
            isClass = YES;
        } else{
            isClass = NO;
        }
        [responseObject writeToFile:PathName atomically:YES];
        success(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * cachePath = PathName;
        if ([[NSFileManager defaultManager] fileExistsAtPath:cachePath]) {
            //从本地读缓存文件
            id responseObject = nil;
            if (isClass) {
                responseObject = [NSMutableArray arrayWithContentsOfFile:cachePath];
            } else {
                responseObject = [NSMutableDictionary dictionaryWithContentsOfFile:cachePath];
            }
            success(responseObject);
        }

    }];
}

本篇和之前写过的关于AFN封装的博客挂钩 如果不懂可以参考一下

http://blog.csdn.net/qq_30402119/article/details/49822513主要就是加下面的四句

    /* 设置安全政策对象 */
     AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    /* 使用 */
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

你可能感兴趣的:(IOS开发,三方类库)