当前位置: 代码迷 >> 综合 >> iOS AFnetWorking 请求webservice数据
  详细解决方案

iOS AFnetWorking 请求webservice数据

热度:81   发布时间:2023-12-22 18:03:09.0

#pragma mark  AFN 请求

- (void)SOAPDataByUrl:(NSString *)url

        webServiceStr:(NSString *)webServiceStr

              success:(void (^)(id responseObject))success

              failure:(void(^)(NSError *error))failure {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

    // 设置请求超时时间

    manager.requestSerializer.timeoutInterval = 30;

    // 返回NSData

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    // 设置请求头

    [manager.requestSerializer setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [manager.requestSerializer setValue:[NSString stringWithFormat:@"%zd", webServiceStr.length] forHTTPHeaderField:@"Content-Length"];

    // 设置HTTPBody

    [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {

        return webServiceStr;

    }];

    [manager POST:url parameters:webServiceStr progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        // 请求成功并且结果有值把结果传出去

        if (success) {

            // 把返回的二进制数据转为字符串

            NSString *newStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

            // XML解析

            id resault =[[[XmlData alloc]init] dataFromXml:newStr execType:@"1"];

            NSLog(@"AFN请求成功 ") ;

            success(resault);

        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        if (failure) {

            NSLog(@"AFN请求失败-%@",error);

            failure(error);

        }

    }];

}



  相关解决方案