由于公司的服务全都是webservice,每次总要花费大量时间在调试服务上面,干脆就写了一个解析wsdl的项目,但愿未来能用上吧。还未通过烘焙,有问题,还请高手点播点播。git
下面,我拿天气服务的wsdl做为例子吧。github
服务的WSDL地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdlweb
WSDL包含如下节点数据结构
definitions 根节点异步
根节点下面有如下节点:post
types 数据类型定义。方法的参数名都包含在里面。ui
message 消息数据结构。spa
portType 描述服务和服务的方法。3d
binding 描述Web Service的通讯协议。调试
service 描述Web Service 的访问点的集合。
下面对来一步一步解析如何根据wsdl 生成SOAP 消息体。
1.添加一个类扩展,以下图DDXMLElement+WSDL.h和DDXMLElement+WSDL.m
头文件中,暴露如下方法
2.SoapUtility 文件是用来封装soap消息的。SoapUtility调用DDXMLElement+WSDL
在SoapUtility头文件中,暴露如下方法
3.服务调用,上面,都把Soap消息给准备好了。那么最后一步就是服务的调用了。这里分两种调用方式:同步和异步。
4.使用方法,下面是天气服务的调用例子
//参数列表
NSDictionary *dic=@{@"theCityName": cityname};
//方法名
NSString *methodName=@"getWeatherbyCityName";
//封装soap信封
SoapUtility *soaputility=[[SoapUtility alloc] initFromFile:@"WeatherWebService"];
NSString *postData=[soaputility BuildSoapwithMethodName:@"getWeatherbyCityName" withParas:dic];
//初始化服务
SoapService *soaprequest=[[SoapService alloc] init];
soaprequest.PostUrl=@"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
soaprequest.SoapAction=[soaputility GetSoapActionByMethodName:methodName SoapType:SOAP];
if (isSync) {
//同步方法
ResponseData *result= [soaprequest PostSync:postData];
[self.result setText:result.Content];
}
else{
//异步请求
[soaprequest PostAsync:postData Success:^(NSString *response) {
[self.result setText:response];
} falure:^(NSError *response) {
[self.result setText:response.description];
}];
}
5.代码实现
https://github.com/xujialiang/SOAP-IOS
欢迎你们给意见。