原帖地址:http://www.cocoachina.com/bbs/read.php?tid-16561.html
最近研究了下soap,找不到iphone上好用的soap代码.发现坛子,关于soap的东西也不多,就把自己写的贴出来,请大家批评指正吧。用到的提供soap接口的网址是:http://www.Nanonull.com/TimeService/这个页面有多个方法可以通过soap调用,页面上也有说明.如果用IE的浏览器还能看到此网页提供的wsdl文件.要做soap的webservice首先要了解一些关于webservice和soap的一些基本知识.下面几个网址可能会帮你快速入门.soap教程:http://www.w3school.com.cn/soap/index.asp使用WSDL发布WebService:http://blog.csdn.net/meiqingsong/archive/2005/04/04/336057.aspx 为了便于理解,我先讲下soap的大体原理:我们在iphone封装soap请求信息,发送到某个提供soap服务的服务器,如下例中我们用到的http://www.Nanonull.com/TimeService/.服务器能接受和识别soap请求,当它接到请求,就根据客户端的请求情况调用服务器上的某个函数,并将函数返回结果封装成soap反馈信息发送给客户端.客户端接收到soap反馈信息后,进行解析处理,以用户能理解的形式呈现给用户.整个过程就这么简单. 好了,假设现在你已经有关于soap的基础知识(没有也没关系,看了例子,再理解就更好理解了),下面我们开始做soap的例子. 第一步,建一个Hello_SOAP项目.用IB将Hello_SOAPViewController.xib做成如下图的界面然后在Hello_SOAPViewController.h中添加如下代码
复制代码 @interface Hello_SOAPViewController : UIViewController{ IBOutlet UITextField *nameInput; IBOutlet UILabel *greeting; NSMutableData *webData; NSMutableString *soapResults; NSXMLParser *xmlParser; BOOL recordResults;}@property(nonatomic, retain) IBOutlet UITextField *nameInput;@property(nonatomic, retain) IBOutlet UILabel *greeting;@property(nonatomic, retain) NSMutableData *webData;@property(nonatomic, retain) NSMutableString *soapResults;@property(nonatomic, retain) NSXMLParser *xmlParser;-(IBAction)buttonClick: (id) sender;- (void)getOffesetUTCTimeSOAP; 然后在Hello_SOAPViewController.xib中将两个输出口和一个动作连接好,这个不用手把手吧? 在Hello_SOAPViewController.m文件中加入以下方法 : 复制代码 - (void)getOffesetUTCTimeSOAP{ recordResults = NO; //封装soap请求消息 NSString *soapMessage = [NSString stringWithFormat: @"<?xml version=/"1.0/" encoding=/"utf-8/"?>/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//">/n" "<soap:Body>/n" "<getOffesetUTCTime xmlns=/"http://www.Nanonull.com/TimeService//">/n" "<hoursOffset>%@</hoursOffset>/n" "</getOffesetUTCTime>/n" "</soap:Body>/n" "</soap:Envelope>/n",nameInput.text ]; NSLog(soapMessage); //请求发送到的路径 NSURL *url = [NSURL URLWithString:@"http://www.nanonull.com/TimeService/TimeService.asmx"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; //以下对请求信息添加属性前四句是必有的,第五句是soap信息。 [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [theRequest addValue: @"http://www.Nanonull.com/TimeService/getOffesetUTCTime" forHTTPHeaderField:@"SOAPAction"]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; //请求 NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; //如果连接已经建好,则初始化data if( theConnection ) { webData = [[NSMutableData data] retain]; } else { NSLog(@"theConnection is NULL"); } } 这个方法作用就是封装soap请求,并向服务器发送请求. 代码有注释.不重复讲解.soap并不难,难的是没有案例告诉我们怎么把其它平台的soap移植过来,这里我给出了代码,我相信对iphone开发人员的话应该能看懂了.我在下面会把此案例的源代码附上.如果自己做不出来再看我的代码.如果我这样讲您觉得不够细,那说明您的iphone开发还不是太深入,那么您应该用不到soap技术.可以飘过了. 下面的代码是接收信息并解析,显示到用户界面 复制代码 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ [webData setLength: 0]; NSLog(@"connection: didReceiveResponse:1");}-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [webData appendData:data]; NSLog(@"connection: didReceiveData:2");}//如果电脑没有连接网络,则出现此信息(不是网络服务器不通)-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"ERROR with theConenction"); [connection release]; [webData release];}-(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"3 DONE. Received Bytes: %d", [webData length]); NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; NSLog(theXML); [theXML release]; //重新加載xmlParser if( xmlParser ) { [xmlParser release]; } xmlParser = [[NSXMLParser alloc] initWithData: webData]; [xmlParser setDelegate: self]; [xmlParser setShouldResolveExternalEntities: YES]; [xmlParser parse]; [connection release]; //[webData release];}-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qNameattributes: (NSDictionary *)attributeDict{ NSLog(@"4 parser didStarElemen: namespaceURI: attributes:"); if( [elementName isEqualToString:@"getOffesetUTCTimeResult"]) { if(!soapResults) { soapResults = [[NSMutableString alloc] init]; } recordResults = YES; } }-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"5 parser: foundCharacters:"); if( recordResults ) { [soapResults appendString: string]; }}-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ NSLog(@"6 parser: didEndElement:"); if( [elementName isEqualToString:@"getOffesetUTCTimeResult"]) { recordResults = FALSE; greeting.text = [[[NSString init]stringWithFormat:@"第%@时区的时间是: ",nameInput.text] stringByAppendingString:soapResults]; [soapResults release]; soapResults = nil; NSLog(@"hoursOffset result"); } }- (void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"-------------------start--------------");}- (void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"-------------------end--------------");} 说明下: 复制代码 -(void)connectionDidFinishLoading:(NSURLConnection *)connection 这个方法是存储接收到的信息 复制代码 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 方法是显示接收的信息. 以上代码已经包括所有用到的代码. 如果一切顺利,应该有如下界面 1.打开界面 2.输入请求的时区 3.显示服务器反馈结果