博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios中webservice报文的拼接
阅读量:5279 次
发布时间:2019-06-14

本文共 5026 字,大约阅读时间需要 16 分钟。

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

    [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

    [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

你可能感兴趣的文章
微软最有价值专家大中华峰会开幕视频
查看>>
Python入门:装饰器
查看>>
MySQL索引类型总结和使用技巧以及注意事项
查看>>
几何球
查看>>
.md5mesh and .md5anim文件介绍
查看>>
文本分类资源和程序开源共享
查看>>
java进阶01 异常处理
查看>>
Django模板层学习笔记
查看>>
开源webos--云DWOS
查看>>
JAVA学习路线图(一文详解)
查看>>
.Net和C#的理解
查看>>
initramfs文件系统
查看>>
jffs2和yaffs2文件系统
查看>>
How to check Logstash's pulse
查看>>
python闭包
查看>>
Leetcode-Letter Combinations of a Phone Number
查看>>
IntelliJ Idea 2017 免费激活方法
查看>>
IOS8 App开发快速入门视频教程与案例分享 20课 附讲义
查看>>
40、mysql的历史简介
查看>>
【java】为什么阿里巴巴禁止在 foreach 循环里进行元素的 remove/add 操作
查看>>