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 ...
随机推荐
- 如何修改Oracle Enterprise Linux时区?
修改/etc/sysconfig/clock [root@psdyy-2 ~]# cat /etc/sysconfig/clock ZONE="Asia/Shanghai" UTC ...
- SpringMVC方法传递集合数组
背景:实体集合作为参数 数据准备: 1.实体类 class A {private int id; private String name; } 2.集合json字符串 [{"id&quo ...
- 虚拟化(三):vsphere套件的安装注意及使用
虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(五):vsphere高可用群集与容错 vsphere套件里面基本的组件有e ...
- dwz 分页 bug (选回 combox 第一个值时不执行 onchange)
先看一下官方的测试: 官方的演示有两个 bug 一个是combox数字一直不变,二是当选回第一个值时不执行 onchange 事件. 经过firebug调试,这是一个bug,传到后台的参数没有得到及时 ...
- SharePoint Online 设置网站集
前言 本文介绍如何在Office 365中设置SharePoint Online网站集,当我们创建好SharePoint Online站点,开始使用之前,一定会有一些基本的设置,本文就为大家介绍这些基 ...
- 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
今天在springmvc集成mybatis时,遇到一个错误 "characterEncoding" 的引用必须以 ';' 分隔符结尾. 这是“&”定义与解析的原因,需要对& ...
- 玩转Eclipse — 自动代码生成的Java Code Template
文章转载地址:点击打开链接 当代码写到一定程度之后,就会发现很多代码都被重复地敲了N多遍,甚至毫不夸张地说:闭着眼睛都能敲出来.大量地敲这些重复地代码,除了锻炼敲键盘的速度,基本上没有其他益处,但是长 ...
- html效果增强
1:提示框 http://keleyi.com/keleyi/phtml/jqplug/ 2:loading效果 <script>function showPage(){ $('#d ...
- 使用jupyter搭建golang的交互式界面:类似于ipython;jupyter还可以使用spark或者结合机器学习
Jupyter Notebook The Jupyter notebook is a web-based notebook environment for interactive computing. ...
- 存储过程参数CHAR传过来null导致超时.
调用的时候不要传NULL,可以传 '' ALTER PROCEDURE [dbo].[up_UC_GetUCExecuteEPList] @Code VARCHAR(3) ,--ch ...