ios中webservice报文的拼接

1、报文头需要密码验证的

- (void)sendAsynWebServiceRequest:(NSString *)nameSpace method:(NSString *)method requestXmlString:(NSString *)requestXmlString

{

    

   

    NSString *sRequestXmlString = [requestXmlString stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];

    sRequestXmlString = [sRequestXmlString stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];

    NSString *soapMessage = [NSString stringWithFormat:

@"<?xml version="1.0" encoding="utf-8" standalone="yes"?> "

"<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:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> "

"<soap:Body> "

                             "<%@  xmlns="%@%@"> "

                             "<xml>%@ %@</xml> "

                             "<verifyID>123456</verifyID> "

                             "</%@> "

"</soap:Body> "

"</soap:Envelope>",method,BASEURL,nameSpace,@"&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;",sRequestXmlString,method];

 

    DMLog(@"发送的soap信息====%@",soapMessage);

    tagName = method;

    

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,nameSpace]];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    [urlRequest setTimeoutInterval:120.0];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    

    [urlRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [urlRequest addValue: [NSString stringWithFormat:@"%@%@/%@",BASEURL,nameSpace,method] forHTTPHeaderField:@"soapAction"];//SOAPAction http://210.51.27.9:8080/dtlp/ws/service

    [urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    2、不需要密码验证的

 

 NSString *sRequestXmlString = [requestXmlString stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];

    sRequestXmlString = [sRequestXmlString stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];

    NSString *soapMessage = [NSString stringWithFormat:

                             @"<?xml version="1.0" encoding="utf-8" standalone="yes"?> "

                             "<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:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> "

                             "<soap:Body> "

                             "<%@  xmlns="%@%@"> "

                             "<xml>%@ %@</xml> "

                             "</%@> "

                             "</soap:Body> "

                             "</soap:Envelope>",method,BASEURL,nameSpace,@"&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;",sRequestXmlString,method];

    

    DMLog(@"发送的soap信息====%@",soapMessage);

    tagName = [NSString stringWithFormat:@"%@Return",method];

    

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASEURL,nameSpace]];

    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

    [urlRequest setTimeoutInterval:120.0];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    

    [urlRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [urlRequest addValue: [NSString stringWithFormat:@"%@%@/%@",BASEURL,nameSpace,method] forHTTPHeaderField:@"soapAction"];//SOAPAction http://210.51.27.9:8080/dtlp/ws/service

    [urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

 

3、解析返回的数据

 

 

-(NSString*)getBusinessXML:(NSString*)xmlStr xmltagName:(NSString*)xmltagName{

    NSString *regEx = [[NSString alloc] initWithFormat:@"<%@ >[\w\W]*</%@>", xmltagName, xmltagName];

    NSString *sxmlstr = [xmlStr stringByReplacingOccurrencesOfString:@"xsi:type="xsd:string"" withString:@""];

    NSRange range = [sxmlstr rangeOfString:regEx options:NSRegularExpressionSearch];

    if(range.location != NSNotFound){

        NSString *businessXML = [sxmlstr substringWithRange:range];

        //替换特殊字符

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&quot;" withString:@"'"];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&apos;" withString:@"""];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];

        businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];

        //去除tagname

       // businessXML = [businessXML substringWithRange:NSMakeRange(tagName.length+2, businessXML.length-(tagName.length*2+5))];

        

        NSRange range1 = [businessXML rangeOfString:@"<?" ];

        NSRange range2 = [businessXML rangeOfString:@"?>"];

        if (range1.location !=NSNotFound && range2.location != NSNotFound) {

            

            NSRange ranng = NSMakeRange(range1.location, range2.location - range1.location + 2);

            businessXML = [businessXML stringByReplacingCharactersInRange:ranng withString:@""];

        }

       

        return businessXML;

    }

    return DATAANALYZEFAIL;

}

    

原文地址:https://www.cnblogs.com/sgdkg/p/4350573.html