码迷,mamicode.com
首页 > 移动开发 > 详细

ios中webservice报文的拼接

时间:2015-03-19 16:15:35      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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

"<soap:Body>\n"

                             "<%@  xmlns=\"%@%@\">\n"

                             "<xml>%@\n%@</xml>\n"

                             "<verifyID>123456</verifyID>\n"

                             "</%@>\n"

"</soap:Body>\n"

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

                             "<soap:Body>\n"

                             "<%@  xmlns=\"%@%@\">\n"

                             "<xml>%@\n%@</xml>\n"

                             "</%@>\n"

                             "</soap:Body>\n"

                             "</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;

}

    

ios中webservice报文的拼接

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!