iOS SOAP 交互

来源:互联网 发布:excel vba 查询数据库 编辑:程序博客网 时间:2024/05/22 14:14

测试服务器

http://www.nanonull.com/TimeService/TimeService.asmx


要传输的xml内容soapXML.xml,不带参数的情况:

<?xml version="1.0" encoding="utf-8"?>
<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:Body>
        <getServerTime xmlns="http://www.Nanonull.com/TimeService/" />
    </soap:Body>
</soap:Envelope>

注意,此xml中不能有注释,如果有注释可能得不到正确的响应


带参数的情况 ,soapXML2.xml

 <?xml version="1.0" encoding="utf-8"?>
 <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:Body>
 <getOffesetUTCTime xmlns="http://www.Nanonull.com/TimeService/">
 <hoursOffset>2.1</hoursOffset>
 </getOffesetUTCTime>
 </soap:Body>
 </soap:Envelope>

控制器头文件

#import <UIKit/UIKit.h>

@interface SendMessageViewController : UIViewController <NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{

        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)getOffesetUTCTimeSOAP; //这个方法使用一个Button控制,可以用xib连线,也可以addTaget

@end

控制器.m文件:

- (void)getOffesetUTCTimeSOAP
{
            recordResults = NO;

            //封装soap请求消息

          //对于刚刚要传送的那两个xml,在整个程序代码中,只需修改两处来实现切换,第一,更改下面这个Path,

        // 第二,[theRequest addValue: @"http://www.Nanonull.com/TimeService/getServerTime" forHTTPHeaderField:@"SOAPAction"];更改这个方法名即可

    NSString *path = [[NSBundle mainBundle] pathForResource:@"soapXML" ofType:@"xml"];
    NSString *soapMessage = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
            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/getServerTime" forHTTPHeaderField:@"SOAPAction"];
    
            //样板二
            //[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");
                    }
            
            
    }



 -(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];
     }