天天看點

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) {

    }];