ios中webservice报文的拼接
1、报文头需要密码验证的
- (void)sendAsynWebServiceRequest:(NSString *)nameSpace method:(NSString *)method requestXmlString:(NSString *)requestXmlString
{
NSString *sRequestXmlString = [requestXmlString stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
sRequestXmlString = [sRequestXmlString stringByReplacingOccurrencesOfString:@">" withString:@">"];
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,@"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",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:@"<"];
sRequestXmlString = [sRequestXmlString stringByReplacingOccurrencesOfString:@">" withString:@">"];
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,@"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",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:@"<" withString:@"<"];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@">" withString:@">"];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@""" withString:@"'"];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
businessXML = [businessXML stringByReplacingOccurrencesOfString:@">" 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;
}
最新文章
- VS2013全攻略
- java 删除所有HTML工具类
- 主机和虚拟机能相互ping通但是不能复制
- HDU 5007 Post Robot KMP (ICPC西安赛区网络预选赛 1001)
- CMD command
- SQL:with ties
- Java Random
- web前端页面性能
- 获取Avrix上Computer Vision and Pattern Recognition的论文,进一步进行统计分析。
- 柔弱的APP如何自我保护,浅谈APP防御手段,使用360加固助手加固/签名/多渠道打包/应用市场发布
- ORACLE窗口函数
- linux操作命令 开发人员需要掌握的一些命令
- Anaconda 虚拟环境的使用
- Docker详解
- python对数据类型的相关操作
- windows sever2003安装Wamp 2.5不成功——VC 11不支持Windows Server 2003和win XP?
- windows 用户变量和系统变量的差别
- xssProject在java web项目中应用
- JavaScript深拷贝—我遇到的应用场景
- UVALive - 6952 DP 分段/隔板
热门文章
- android获取com.android.internal.R
- js验证表单大全
- 第五节:AppDomain FirstChance异常通知
- IEnumerable 接口 实现foreach 遍历 实例
- Github上LeakCanary编译报错CreateProcess error=2的解决方法
- C高级 服务器内核分析和构建 (一)
- 多线程报表生成其中报表以pdf形式保存
- Python学习——struct模块的pack、unpack示例
- ultraedit高亮显示设置
- [原]Java修炼 之 基础篇(二)Java语言构成