iOS webservice SOAP 请求

1. Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。Web Service是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。

简单来说 webservice 请求就是是往服务器POST XML数据;

然后服务器响应得到的也是XML数据;

2. 如下使用iOS NSUrlConnection 接口进行webservice 请求示例;

 测试使用的接口 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

 根据接口描述使用了两种版本的soap请求:soap1.1,soap1.2 两种区别在于 soap请求体xml的格式上;

实现原理其实根据接口上的soap体描述组合xml包;

    //SOAP 1.1
- (void)soapv11Request
{
    NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"" forKey:@""];
    [req setValue:@"http://WebXml.com.cn/getMobileCodeInfo" forHTTPHeaderField:@"SOAPAction"];
    
    NSString *reqBody = [NSString stringWithFormat:@"<?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>
                         <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
                         <mobileCode>%@</mobileCode>
                         <userID></userID>
                         </getMobileCodeInfo>
                         </soap:Body>
                         </soap:Envelope>",self.phoneNumTF.text];

    NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
    req.HTTPBody = reqData;
    
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError) {
            NSLog(@"Error:%@",connectionError);
        }
        else
        {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }
        
        
    }];
}
//SOAP 1.2
- (void)soapv12Request
{
    NSURL *url = [NSURL URLWithString:@"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    req.HTTPMethod = @"POST";
    [req setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    NSString *reqBody = [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>
                         <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
                         <mobileCode>%@</mobileCode>
                         <userID></userID>
                         </getMobileCodeInfo>
                         </soap12:Body>
                         </soap12:Envelope>",self.phoneNumTF.text];
    
    NSData *reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
    req.HTTPBody = reqData;
    
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError) {
            NSLog(@"Error:%@",connectionError);
        }
        else
        {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }
        
        
    }];
}

   示例工程下载:https://github.com/cocoajin/TDDDemo/tree/master/WebServiceTest

3. 在实际应用中,我们可以使用一些xml处理的相关第三方类库来组合soap体;

   组合好soap请求体之后,往指定接口POST 请求数据即可;

   也有专门处理SOAP请求的类库:https://github.com/priore/SOAPEngine 可以了解一下

原文地址:https://www.cnblogs.com/cocoajin/p/6518024.html