美文网首页
Objective-C通过AFN使用Soap来请求webServ

Objective-C通过AFN使用Soap来请求webServ

作者: 达菲栀子 | 来源:发表于2017-08-25 15:48 被阅读229次

    公共方法

    /**
     *  请求SOAP,返回NSData
     *
     *  @param url      请求地址
     *  @param soapBody soap的XML中方法和参数段
     *  @param success  成功block
     *  @param failure  失败block
     */
    + (void)SOAPData:(NSString *)url soapBody:(NSString *)soapBody success:(void (^)(id responseObject))success failure:(void(^)(NSError *error))failure {
        
        NSString *soapStr = [NSString stringWithFormat:
                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
                             <soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\
                             xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\
                             <soap:Header>\
                             </soap:Header>\
                             <soap:Body>%@</soap:Body>\
                             </soap:Envelope>",soapBody];
        
        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"];
        // 设置HTTPBody
        [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
            return soapStr;
        }];
        
        [manager POST:url parameters:soapStr success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            // 把返回的二进制数据转为字符串
            NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            // 利用正则表达式取出<GetInventoryListResult></GetInventoryListResult>之间的字符串
            NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=GetInventoryListResult\\>).*(?=</GetInventoryListResult>)" options:NSRegularExpressionCaseInsensitive error:nil];
            NSDictionary *dict = [NSDictionary dictionary];
            for (NSTextCheckingResult *checkingResult in [regular matchesInString:result options:0 range:NSMakeRange(0, result.length)]) {
                // 得到字典
                dict = [NSJSONSerialization JSONObjectWithData:[[result substringWithRange:checkingResult.range] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
            }
            // 请求成功并且结果有值把结果传出去
            if (success && dict) {
                success(dict);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (failure) {
                failure(error);
            }
        }];
    }
    

    调用传参

     NSString *bodyString =  [NSString stringWithFormat:
                                     @"<GetInventoryList xmlns=\"http://WmsService/\">\
                                     <materialRequisitionNum>%@</materialRequisitionNum>\
                                     </GetInventoryList>",self.beforeSelectedMaterialNum];
            
            [LSHttpTool SOAPData:PiKingKB_getSLDetail soapBody:bodyString success:^(id responseObject) {
                NSLog(@"%@", responseObject);
            } failure:^(NSError *error) {
                NSLog(@"%@", error);
            }];
    

    通过正则表达式取出XML节点数据,正则表达式:(?<=GetInventoryListResult\>).*(?=</GetInventoryListResult>)

    NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            // 利用正则表达式取出<GetInventoryListResult></GetInventoryListResult>之间的字符串
            NSRegularExpression *regular = [[NSRegularExpression alloc] initWithPattern:@"(?<=GetInventoryListResult\\>).*(?=</GetInventoryListResult>)" options:NSRegularExpressionCaseInsensitive error:nil];
            NSDictionary *dict = [NSDictionary dictionary];
            for (NSTextCheckingResult *checkingResult in [regular matchesInString:result options:0 range:NSMakeRange(0, result.length)]) {
                // 得到字典
                dict = [NSJSONSerialization JSONObjectWithData:[[result substringWithRange:checkingResult.range] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
            }
    将要被取出的XML数据
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
    <GetInventoryListResponse xmlns="http://WmsService/">
    <GetInventoryListResult>
    [
    {"Guid":"AD6BE916-D7A6-47D9-A54B-30D99FAEA84D","FManuOrder":"SDCM170718003","ImaterialRequisitionNum":"REO17071700022","IquantityRequired":2.71,"Iunit":"PCS","MaterieId":"582-A210172","OweMaterialQty":2.71,"StorageId":"3955","StorageName":"LS_HD-WIP-A-1-03","StoreaeQty":100.0000},
    {"Guid":"82778F24-5071-4521-9BD0-A3218E799154","FManuOrder":"SDCM170718003","ImaterialRequisitionNum":"REO17071700022","IquantityRequired":8.13,"Iunit":"PCS","MaterieId":"582-A210140","OweMaterialQty":8.13,"StorageId":"3957","StorageName":"LS_HD-WIP-A-1-05","StoreaeQty":100.0000},
    {"Guid":"BE98835F-F1A7-4B6C-B8C8-6827AAFD2DF7","FManuOrder":"SDCM170718003","ImaterialRequisitionNum":"REO17071700022","IquantityRequired":10.16,"Iunit":"PCS","MaterieId":"581-A130112","OweMaterialQty":10.16,"StorageId":"3959","StorageName":"LS_HD-WIP-A-1-07","StoreaeQty":100.0000}
    ]
    </GetInventoryListResult>
    </GetInventoryListResponse>
    </soap:Body>
    </soap:Envelope>
    

    相关文章

      网友评论

          本文标题:Objective-C通过AFN使用Soap来请求webServ

          本文链接:https://www.haomeiwen.com/subject/ulgudxtx.html