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 可以了解一下
iOS webservice SOAP 请求的更多相关文章
- Java发布一个简单 webservice应用 并发送SOAP请求
一.创建并发布一个简单的webservice应用 1.webservice 代码: package com.ls.demo; import javax.jws.WebMethod; import ja ...
- Java发布webservice应用并发送SOAP请求调用
webservice框架有很多,比如axis.axis2.cxf.xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML ...
- WebService SOAP、Restful和HTTP(post/get)请求区别
web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力. Web service 就是一个应用程序,它向外界暴露出一个能够通过We ...
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
http://www.itnose.net/detail/6189456.html 浅谈WebService SOAP.Restful.HTTP(post/get)请求 2015-01-09 19:2 ...
- Jmeter发送SOAP请求对WebService接口测试
Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe, application/soap+xml;c ...
- Axis2(10):使用soapmonitor模块监视soap请求与响应消息
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...
- PHP用post来进行Soap请求
最近调了一个Soap请求C# webservice的项目.网上坑不少. 使用原生的SoapClient库请求也是失败.只好用post来进行模拟了.代码贴出来,给大家参考一下. <?php nam ...
- webService(SOAP)性能测试脚本
本文以天气预报的webService为基础进行学习 webService地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx ...
- [转]C#通过Http发送Soap请求
/// <summary> /// 发送SOAP请求,并返回响应xml /// </summary> /// <param na ...
随机推荐
- 使用Coding4Fun工具包
Coding4Fun是一款很受WP开发者喜爱的开源类库,对于开发者来说,Coding4Fun上手很简单.只要从CodePlex下载Coding4Fun工具包,下载完成后,解压文件到一个文件夹中,里面有 ...
- VirtualBox - NAT虚拟机访问外网 + Host-Only物理主机虚拟机互访
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-System_eth0 # 未手动设定HOST-ONLY静态IP时的默认值 #T ...
- System.loadLibrary()的使用方法汇总
当使用System.loadLibrary()调用 Dll,两种方法: 1.设定环境变量. 比如:所编辑的Dll在目录“D:/cppProjects/nativecode/release”内,将这个路 ...
- Swift - EasingAnimation绘制圆环动画
Swift - EasingAnimation绘制圆环动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // CircleView ...
- UITableViewCell图片视差效果
UITableViewCell图片视差效果 效果 源码 https://github.com/YouXianMing/Animations 细节 OffsetImageCell.m OffsetCel ...
- 实用ExtJS教程100例-010:ExtJS Form异步加载和提交数据
ExtJS Form 为我们提供了两个方法:load 和 submit,分别用来加载和提交数据,这两个方法都是异步的. 系列ExtJS教程持续更新中,点击查看>>最新ExtJS教程目录 F ...
- 详细解读Android中的搜索框(二)—— Search Dialog
Search Dialog是提供搜索的控件之一,还有一个是上次小例子给出的searchView,关于SearchView的东西后面会说到.本次先从Search Dialog说起,让大家慢慢理解andr ...
- Button 自定义图片,代码绘制样式,添加音效的方法
Button自己在xml文件中绑定监听器 <!-- 设定onclick属性,然后在activity中定义相应的方法 --> <!-- 通过xml布局中通过button的android ...
- 【周年庆】china-pub 14周年庆感恩回馈四波狂热来袭
活动主题:china-pub 14周年庆感恩回馈四波狂热来袭活动说明:[第1波]电子书免费抢!10万好书 65折封顶! 活动规则: 1.活动期间内凡 ...
- Java多线程之Callable接口与Runnable的实现以及选择
通过实现Runnable接口的实现 package Thread; import java.util.concurrent.ExecutorService;import java.util.concu ...