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 可以了解一下

iOS webservice SOAP 请求的更多相关文章

  1. Java发布一个简单 webservice应用 并发送SOAP请求

    一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...

  2. Java发布webservice应用并发送SOAP请求调用

    webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...

  3. WebService SOAP、Restful和HTTP(post/get)请求区别

    web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力. Web service 就是一个应用程序,它向外界暴露出一个能够通过We ...

  4. 浅谈WebService SOAP、Restful、HTTP(post/get)请求

    http://www.itnose.net/detail/6189456.html 浅谈WebService SOAP.Restful.HTTP(post/get)请求 2015-01-09 19:2 ...

  5. Jmeter发送SOAP请求对WebService接口测试

    Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe,  application/soap+xml;c ...

  6. Axis2(10):使用soapmonitor模块监视soap请求与响应消息

    在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...

  7. PHP用post来进行Soap请求

    最近调了一个Soap请求C# webservice的项目.网上坑不少. 使用原生的SoapClient库请求也是失败.只好用post来进行模拟了.代码贴出来,给大家参考一下. <?php nam ...

  8. webService(SOAP)性能测试脚本

    本文以天气预报的webService为基础进行学习   webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ...

  9. [转]C#通过Http发送Soap请求

    /// <summary>        /// 发送SOAP请求,并返回响应xml        /// </summary>        /// <param na ...

随机推荐

  1. linux内核netfilter模块分析之:HOOKs点的注册及调用

    转自;http://blog.csdn.net/suiyuan19840208/article/details/19684883 -1: 为什么要写这个东西?最近在找工作,之前netfilter 这一 ...

  2. websocket消息推送实现

    一.服务层 package com.demo.websocket; import java.io.IOException; import java.util.Iterator; import java ...

  3. Android之代码创建布局

    大概描述一下效果:最外层是一个 RelativeLayout 里面有自定义个LinearLayout,每个LinearLayout有两个TextView.that's it !!! private v ...

  4. 详细解读Android中的搜索框(二)—— Search Dialog

    Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...

  5. django的权限认证:登录和退出。auth模块和@login_required装饰器

    在settings.py中配置LOGIN_URL参数: # 用户访问带有(@login_required)标签的页面(view)时,如果没有登录,就会跳转到LOGIN_URL(即登陆url). LOG ...

  6. Oracle简单的备份和恢复-导出和导入(1)

    ylbtech-Oracle:Oracle简单的备份和恢复-导出和导入(1) Oracle简单的备份和恢复-导出和导入 1. 用户导出自己的表(emp,dept)返回顶部 1.1, 我们启动Oracl ...

  7. 网关局域网通信协议V2.0

    http://docs.opencloud.aqara.cn/development/gateway-LAN-communication/ https://github.com/aqara/openc ...

  8. NLP的一些学习资料

    结巴分词和NLTK----一套中文文本分析的组合拳 https://www.jianshu.com/p/aea87adee163 比较好的情感分析的文章 https://www.cnblogs.com ...

  9. Python二维数组构造

    周末用python要写个算法用到来二维数组, 一时间还不知道python怎么构造多维数组出来.看到一段不错的代码, 记录一下. Python使用list嵌套实现多维数组, PHP可以使用array嵌套 ...

  10. Python json模块dumps loads

    python中json数据的使用. dumps和loads也是需要成对使用的,就像c++ new/delete malloc/free一样需要成对使用. 看着像json的字符串,也不一定是json字符 ...