iOS下WebService接口调用与解析-一、Soap对象的封装
2017-03-24 17:15
495 查看
封装Soap对象
Soap对象是对请求体的简单封装,免去重复而又容易出错的拼接工作。首先是头文件:SOAPMessage.h
#import <Foundation/Foundation.h> @interface SOAPMessage : NSObject @property (nonatomic, strong) NSString *nameSpace; @property (nonatomic, strong) NSString *methodName; @property (nonatomic, strong) NSDictionary *params; - (SOAPMessage *)initWithNameSpace:(NSString *)nameSpace withMethodName:(NSString *)method withParams:(NSDictionary *)params; /** * 创建SOAP消息,内容格式就是网站上提示的请求报文的实体主体部分 */ - (NSString *)getSoapMessage; @end
头文件中,定义了三个属性,分别为:
nameSpace:命名空间
methodName:方法名
params:方法名对应的参数列表
一个初始化方法:
- (SOAPMessage )initWithNameSpace:(NSString )nameSpace withMethodName:(NSString )method withParams:(NSDictionary )params;
包含三个参数,对应定义的三个属性。
最后一个方法的作用是将封装后的SOAP类型的xml对象转换成NSString。
然后是实现:SOAPMessage.m
#import "SOAPMessage.h" @interface SOAPMessage () - (NSString *) generateMethod; - (NSString *) generateParams:(NSDictionary *)dict; @end @implementation SOAPMessage - (SOAPMessage *)initWithNameSpace:(NSString *)nameSpace withMethodName:(NSString *)methodName withParams:(NSDictionary *)params { if(self =[super init]) { self.nameSpace = nameSpace; self.methodName = methodName; self.params = params; } return self; } - (NSString *)getSoapMessage { NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap12:Envelope " "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "%@" //"<say xmlns=\"http://service.core.soft.com\">" //"<name>%@</name>" //"</say>" //"<getSupportCity xmlns=\"http://WebXml.com.cn/\">" //"<byProvinceName>%@</byProvinceName>" //"</getSupportCity>" "</soap12:Body>" "</soap12:Envelope>", [self generateMethod]]; return soapMsg; } - (NSString *) generateMethod { NSString *result = [[NSString alloc] initWithFormat:@"<%@ xmlns=\"%@\">%@</%@>", [self methodName], [self nameSpace], [self generateParams:[self params]], [self methodName]]; return result; } - (NSString *) generateParams:(NSDictionary *)dic { NSMutableString *params = [NSMutableString string]; for (id key in [dic allKeys]) { NSString *param = @"<%@>%@</%@>"; NSString *p = [[NSString alloc]initWithFormat:param, key, [dic objectForKey:key], key]; [params appendString:p]; } return params; } @end
相关文章推荐
- 如何用IOS调用WebService(SOAP接口)
- ios webservice编程:soap封装调用,返回xml,json解析
- 如何用IOS调用WebService(SOAP接口)
- 如何用IOS调用WebService(SOAP接口)
- [置顶] iOS调用WebService(SOAP接口)
- python通过http请求发送soap报文进行webservice接口调用
- axis1.4 客户端调用webservice接口返回错误:no SOAPAction header!
- 动态webservice调用接口并读取解析返回结果
- java 调用 webService Xfire接口,xml解析为object
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- iOS 调用Webservice接口
- cxf 解析调用外部soap类型的webservice
- 用ladon框架封装Python为Webservice接口以及调用接口的方法
- php调用webservice接口封装方法
- 使用URL工具类调用webservice接口(soap)与http接口的实现方式
- iOS --调用webservice接口
- iOS使用WSDL2ObjC工具调用Webservice接口
- 关于SOAP调用返回对象的写法 wsdl webservice
- ios 进阶 -- 调用webservice接口(soap协议)的方法
- C#编写webservice接口、调用以及解析数据