iOS 如何封装嵌套的SOAP请求

来源:互联网 发布:淘宝怎么买电鸡 编辑:程序博客网 时间:2024/06/10 04:33

<?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>    <MCSInterface xmlns="http://tempuri.org/">      <dllName>string</dllName>      <className>string</className>      <MethodName>string</MethodName>      <args>        <anyType />        <anyType />      </args>    </MCSInterface>  </soap:Body></soap:Envelope>

对于此类SOAP协议的请求,应该是包含两层SOAP请求封装,封装后的请求应该是:

<?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><MCSInterface xmlns="http://tempuri.org/"><dllName xsi:type = "xsd:string">SysMaintain_Service</dllName><className xsi:type = "xsd:string">SysMaintain_Service.MySystem.SysMaintain</className><MethodName xsi:type = "xsd:string">getWebModules</MethodName><args xsi:type = "MCSInterface" xmlns="http://tempuri.org/"><anyType xsi:type = "xsd:int">62</anyType><anyType xsi:type = "xsd:string"></anyType></args></MCSInterface></soap:Body></soap:Envelope>


调用请求的代码如下:

 NSLog(@"=======同步请求开始======\n");

    NSMutableArray *arr1=[NSMutableArrayarray];

    NSMutableArray *args1 = [NSMutableArrayarray];

    [arr1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:@"SysMaintain_Service",@"dllName",nil]];

    [arr1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:@"SysMaintain_Service.MySystem.SysMaintain",@"className",nil]];

    [arr1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:@"getWebModules",@"MethodName",nil]];

    [args1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInt:32],@"anyType",nil]];

    [args1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:@"",@"anyType",nil]];

    [arr1 addObject:[NSDictionarydictionaryWithObjectsAndKeys:args1,@"args",nil]];

    NSString *soapMsg1=[SoapHelperarrayToNameSpaceSoapMessage:arr1space:@"http://tempuri.org/"methodName:@"MCSInterface"];

    

    NSLog(@"soapMsg = %@", soapMsg1);

    

    NSString *xml1 =[helpersyncServiceMethod:@"MCSInterface"soapMessage:soapMsg1];

    NSLog(@"同步请求返回的xml:\n%@\n",xml1);

    




然后提交请求,封装SOAPMsg的代码如下,可以应对多层嵌套:

SOAPHelper.h


#import <Foundation/Foundation.h>


@interface SoapHelper :NSObject

//默认soap信息

+(NSString*)defaultSoapMesage;

//生成soap信息

+(NSString*)methodSoapMessage:(NSString*)methodName;

+(NSString*)nameSpaceSoapMessage:(NSString*)space methodName:(NSString*)methodName;

+(NSString*)paramToDefaultSoapMessage:(NSString*)key methodName:(NSString*) methodName;

+(NSString*)paramToSoapMessage:(NSString*)space key:(NSString*)key methodName:(NSString*) methodName;

//有参数soap生成

+(NSString*)arrayToDefaultSoapMessage:(NSArray*)arr methodName:(NSString*)methodName;

+(NSString*)arrayToDefaultSoapMessage:(NSArray *)arr key:(NSString *)key methodName:(NSString*)methodName;

+(NSString*)arrayToNameSpaceSoapMessage:(NSArray*)arr space:(NSString*)space methodName:(NSString*)methodName;

+(NSString*)arrayToNameSpaceSoapMessage:(NSArray *)arr space:(NSString*)space key:(NSString *)key methodName:(NSString*)methodName;

@end


SOAPHelper.m


#import "SoapHelper.h"

@implementation SoapHelper

+(NSString*)defaultSoapMesage{

   NSString *soapBody=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"

    "<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/\">\n"

    "<soap:Body>%@</soap:Body></soap:Envelope>";

    return soapBody;

}

+(NSString*)methodSoapMessage:(NSString*)methodName{

    NSMutableString *soap=[NSMutableStringstringWithFormat:@"<%@ xmlns=\"%@\">",methodName,defaultWebServiceNameSpace];

    [soap appendString:@"%@"];

    [soap appendFormat:@"</%@>",methodName];

    return [NSStringstringWithFormat:[selfdefaultSoapMesage],soap];

}


+(NSString*)paramToDefaultSoapMessage:(NSString*)key methodName:(NSString*) methodName{

    NSMutableString *soap=[NSMutableStringstringWithFormat:@"<%@ xsi:type = \"%@\" xmlns=\"%@\">",key, methodName,defaultWebServiceNameSpace];

    [soap appendString:@"%@"];

    [soap appendFormat:@"</%@>",key];

    return [NSStringstringWithFormat:@"%@", soap];

}


+(NSString*)paramToSoapMessage:(NSString*)space key:(NSString*)key methodName:(NSString*) methodName {

    NSMutableString *soap=[NSMutableStringstringWithFormat:@"<%@ xsi:type = \"%@\" xmlns=\"%@\">", key, methodName, space];

    [soap appendString:@"%@"];

    [soap appendFormat:@"</%@>",key];

    return [NSStringstringWithFormat:@"%@", soap];

}


+(NSString*)nameSpaceSoapMessage:(NSString*)space methodName:(NSString*)methodName{

    NSMutableString *soap=[NSMutableStringstringWithFormat:@"<%@ xmlns=\"%@\">",methodName,space];

    [soap appendString:@"%@"];

    [soap appendFormat:@"</%@>",methodName];

    return [NSStringstringWithFormat:[selfdefaultSoapMesage],soap];

}


+(NSString *)arrayToDefaultSoapMessage:(NSArray *)arr key:(NSString *)key methodName:(NSString*)methodName{

    if ([arr count]==0||arr==nil) {

        return [NSStringstringWithFormat:[selfparamToDefaultSoapMessage:keymethodName:methodName],@""];

    }

    NSMutableString *msg=[NSMutableStringstringWithFormat:@""];

    NSString *type;

    for (NSDictionary *itemin arr) {

        NSString *key=[[itemallKeys]objectAtIndex:0];

        id itemObject = [itemobjectForKey:key];

        if([itemObjectisKindOfClass:[NSArrayclass]]) {

            msg = [NSMutableStringstringWithString:[selfarrayToDefaultSoapMessage:itemObjectkey:keymethodName:methodName]];

            continue;

        }

        else if([itemObject isKindOfClass:[NSNumberclass]])

        {

            constchar * pObjCType = [(NSNumber*)itemObjectobjCType];

            if (strcmp(pObjCType,@encode(int))  ==0) {

                type = @"int";

            }

            if (strcmp(pObjCType,@encode(float)) ==0) {

                type = @"float";

            }

            if (strcmp(pObjCType,@encode(double))  ==0) {

                type = @"double";

            }

            if (strcmp(pObjCType,@encode(BOOL)) ==0) {

                type = @"bool";

            }

        }

        else if ([itemObject isKindOfClass:[NSStringclass]])

            type = @"string";

        

        [msg appendFormat:@"<%@ xsi:type = \"xsd:%@\">", key, type];

        [msg appendString:[NSStringstringWithFormat:@"%@",[itemobjectForKey:key]]];

        [msg appendFormat:@"</%@>",key];

        

    }

    return [NSStringstringWithFormat:[selfparamToDefaultSoapMessage:keymethodName:methodName],msg];


    

}



+(NSString*)arrayToDefaultSoapMessage:(NSArray*)arr methodName:(NSString*)methodName{

    if ([arr count]==0||arr==nil) {

        return [NSStringstringWithFormat:[selfmethodSoapMessage:methodName],@""];

    }

    NSMutableString *msg=[NSMutableStringstringWithFormat:@""];

    NSString *type;

    for (NSDictionary *itemin arr) {

        NSString *key=[[itemallKeys]objectAtIndex:0];

        id itemObject = [itemobjectForKey:key];

        if([itemObjectisKindOfClass:[NSArrayclass]]) {

            [msg appendString:[selfarrayToDefaultSoapMessage:itemObjectkey:keymethodName:methodName]];

            continue;

        }

        else if([itemObject isKindOfClass:[NSNumberclass]])

        {

            constchar * pObjCType = [(NSNumber*)itemObjectobjCType];

            if (strcmp(pObjCType,@encode(int))  ==0) {

                type = @"int";

            }

            if (strcmp(pObjCType,@encode(float)) ==0) {

                type = @"float";

            }

            if (strcmp(pObjCType,@encode(double))  ==0) {

                type = @"double";

            }

            if (strcmp(pObjCType,@encode(BOOL)) ==0) {

                type = @"bool";

            }

        }

        else if ([itemObject isKindOfClass:[NSStringclass]])

            type = @"string";

            

        [msg appendFormat:@"<%@ xsi:type = \"xsd:%@\">", key, type];

        [msg appendString:[NSStringstringWithFormat:@"%@",[itemobjectForKey:key]]];

        [msg appendFormat:@"</%@>",key];


    }

    return [NSStringstringWithFormat:[selfmethodSoapMessage:methodName],msg];

}


+(NSString*)soapMsgAppendToSoapMSG:(NSString*)space methodName:(NSString*)methodName soapMsg:(NSString*)soapMsg{

    if([soapMsglength] ==0)

        return [NSStringstringWithFormat:[selfnameSpaceSoapMessage:spacemethodName:methodName],@""];

    return [NSStringstringWithFormat:[selfnameSpaceSoapMessage:spacemethodName:methodName], soapMsg];

}


+(NSString *)arrayToNameSpaceSoapMessage:(NSArray *)arr space:(NSString*)space key:(NSString *)key methodName:(NSString*)methodName{

    if ([arr count]==0||arr==nil) {

        return [NSStringstringWithFormat:[selfparamToSoapMessage:spacekey:key methodName:methodName],@""];

    }

    NSMutableString *msg=[NSMutableStringstringWithFormat:@""];

    NSString *type;

    for (NSDictionary *itemin arr) {

        NSString *key=[[itemallKeys]objectAtIndex:0];

        id itemObject = [itemobjectForKey:key];

        if([itemObjectisKindOfClass:[NSArrayclass]]) {

            msg = [NSMutableStringstringWithString:[selfarrayToNameSpaceSoapMessage:itemObjectspace:spacekey:keymethodName:methodName]];

            continue;

        }

        else if([itemObject isKindOfClass:[NSNumberclass]])

        {

            constchar * pObjCType = [(NSNumber*)itemObjectobjCType];

            if (strcmp(pObjCType,@encode(int))  ==0) {

                type = @"int";

            }

            if (strcmp(pObjCType,@encode(float)) ==0) {

                type = @"float";

            }

            if (strcmp(pObjCType,@encode(double))  ==0) {

                type = @"double";

            }

            if (strcmp(pObjCType,@encode(BOOL)) ==0) {

                type = @"bool";

            }

        }

        else if ([itemObject isKindOfClass:[NSStringclass]])

            type = @"string";

        

        [msg appendFormat:@"<%@ xsi:type = \"xsd:%@\">", key, type];

        [msg appendString:[NSStringstringWithFormat:@"%@",[itemobjectForKey:key]]];

        [msg appendFormat:@"</%@>",key];

        

    }

    return [NSStringstringWithFormat:[selfparamToSoapMessage:spacekey:key methodName:methodName],msg];

    

    

}



+(NSString*)arrayToNameSpaceSoapMessage:(NSArray*)arr space:(NSString*)space methodName:(NSString*)methodName{

    if ([arr count]==0||arr==nil) {

        return [NSStringstringWithFormat:[selfmethodSoapMessage:methodName],@""];

    }

    NSMutableString *msg=[NSMutableStringstringWithFormat:@""];

    NSString *type;

    for (NSDictionary *itemin arr) {

        NSString *key=[[itemallKeys]objectAtIndex:0];

        id itemObject = [itemobjectForKey:key];

        if([itemObjectisKindOfClass:[NSArrayclass]]) {

            [msg appendString:[selfarrayToNameSpaceSoapMessage:itemObjectspace:spacekey:keymethodName:methodName]];

            continue;

        }

        else if([itemObject isKindOfClass:[NSNumberclass]])

        {

            constchar * pObjCType = [(NSNumber*)itemObjectobjCType];

            if (strcmp(pObjCType,@encode(int))  ==0) {

                type = @"int";

            }

            if (strcmp(pObjCType,@encode(float)) ==0) {

                type = @"float";

            }

            if (strcmp(pObjCType,@encode(double))  ==0) {

                type = @"double";

            }

            if (strcmp(pObjCType,@encode(BOOL)) ==0) {

                type = @"bool";

            }

        }

        else if ([itemObject isKindOfClass:[NSStringclass]])

            type = @"string";

        

        [msg appendFormat:@"<%@ xsi:type = \"xsd:%@\">", key, type];

        [msg appendString:[NSStringstringWithFormat:@"%@",[itemobjectForKey:key]]];

        [msg appendFormat:@"</%@>",key];

        

    }

    return [NSStringstringWithFormat:[selfnameSpaceSoapMessage:spacemethodName:methodName],msg];

}


@end





0 0
原创粉丝点击