天天看点

iOS开发调用 webSerVice接口 详解

传参:GetFreeKeChListMethod:@"GetFreeLessonsLst" soapBody:@"<pageIndex>1</pageIndex><pageSize>10</pageSize>"

请求

  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:Body><%@ xmlns=\"*******你的命名空间**********\">%@</%@></soap:Body>\

                         </soap:Envelope>",method,soapBody,method];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    // 设置请求头

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

    NSLog(@"++++%@",soapStr);

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

    // 设置HTTPBody

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

        return soapStr;

    }];

    [manager POST:url parameters:soapStr progress:^(NSProgress * _Nonnull downloadProgress) {

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

      //解析

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

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

        NSString * newResult = [[result componentsSeparatedByString:[NSString stringWithFormat:@"<%@Result>",method]].lastObject componentsSeparatedByString:[NSString stringWithFormat:@"</%@Result>",method]].firstObject;

         NSLog(@"++++%@",newResult);

        callWebback(newResult,nil);

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

    }];